<?php declare(strict_types=1);
namespace DreiscCmsPro\Subscriber\Installment\PluginConfig;
use DreiscCmsPro\Core\PluginConfig\PluginConfigFetcher;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Storefront\Page\Navigation\NavigationPage;
use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
use Shopware\Storefront\Page\Page;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPage;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PluginConfigSubscriber implements EventSubscriberInterface
{
final public const DREISC_CMS_PRO_INSTALLMENT_PLUGIN_CONFIG_DATA = 'DREISC_CMS_PRO_INSTALLMENT_PLUGIN_CONFIG_DATA';
public function __construct(private readonly PluginConfigFetcher $pluginConfigFetcher)
{
}
public static function getSubscribedEvents(): array
{
return [
NavigationPageLoadedEvent::class => 'addInstallment'
];
}
/**
* @throws InconsistentCriteriaIdsException
*/
public function addInstallment(PageLoadedEvent $event)
{
/** @var Page|ProductPage|NavigationPage $page */
$page = $event->getPage();
$pluginConfig = $this->pluginConfigFetcher->getPluginConfigArray();
$pluginConfigStruct = new PluginConfigStruct(
$pluginConfig['googleApiKey'] ?? ''
);
$page->addExtension(
self::DREISC_CMS_PRO_INSTALLMENT_PLUGIN_CONFIG_DATA,
$pluginConfigStruct
);
}
}