<?php declare(strict_types=1);
namespace zenit\PlatformDataBadges\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\Uuid\Exception\InvalidUuidException;
use Shopware\Core\System\SystemConfig\Exception\InvalidDomainException;
use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
use zenit\PlatformDataBadges\Struct\SystemConfigData;
/**
* Class ProductPageLoadedSubscriber
* @package zenit\PlatformDataBadges\Storefront\Page\Product\Subscriber
*/
class Subscriber implements EventSubscriberInterface
{
/**
* @var string
*/
private $pluginName = 'zenitPlatformDataBadges';
/**
* @var string
*/
private $configPath = 'zenitPlatformDataBadges.config.';
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* ProductPageLoadedSubscriber constructor.
* @param SystemConfigService $systemConfigService
*/
public function __construct(SystemConfigService $systemConfigService)
{
$this->systemConfigService = $systemConfigService;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return[
'sales_channel.product.loaded' => 'onProductLoaded'
];
}
/**
* @param ProductPageLoadedEvent|NavigationPageLoadedEvent|SearchPageLoadedEvent $event
* @throws InconsistentCriteriaIdsException
* @throws InvalidDomainException
* @throws InvalidUuidException
*/
public function onProductLoaded($event): void
{
$shopId = $event->getSalesChannelContext()->getSalesChannel()->getId();
// is active check
if (!$this->systemConfigService->get($this->configPath . 'active', $shopId)) {
return;
}
// get config
$systemConfig = $this->systemConfigService->getDomain($this->configPath, $shopId, true);
// replace domainstrings in keys
$config = [];
foreach($systemConfig as $key => $value) {
$config[str_replace($this->configPath, '',$key)] = $value;
}
// set config
$configValues = new SystemConfigData($config);
// add config & custom fields
$event->getContext()->addExtensions([
$this->pluginName => $configValues
]
);
}
}