<?php
declare(strict_types=1);
namespace Lnb\Shopware6\LnbTrustedShopsRates\Subscriber;
use Lnb\Shopware6\LnbTrustedShopsRates\Service\TrustedShopsService;
use Lnb\Shopware6\LnbTrustedShopsRates\Struct\TrustedShopsRates;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class FrontEndSubscriber implements EventSubscriberInterface
{
protected ContainerInterface $container;
protected TrustedShopsService $service;
protected SystemConfigService $systemConfigService;
public function __construct(
ContainerInterface $container,
TrustedShopsService $service,
SystemConfigService $systemConfigService,
) {
$this->container = $container;
$this->service = $service;
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents()
{
return [
HeaderPageletLoadedEvent::class => ['onHeaderPageletLoaded'],
FooterPageletLoadedEvent::class => ['onFooterPageletLoaded'],
];
}
public function onFooterPageletLoaded(FooterPageletLoadedEvent $event)
{
$footerEnabled = $this->systemConfigService->get('LnbTrustedShopsRates.config.enableFooter');
if (!$footerEnabled) {
return null;
}
$rates = $this->service->loadAndSaveTrustedShopsRates();
if (!$rates) {
return;
}
$trustedShopsRates = new TrustedShopsRates($rates);
$event->getPagelet()->addExtension('trustedShops', $trustedShopsRates);
}
public function onHeaderPageletLoaded(HeaderPageletLoadedEvent $event)
{
$headerEnabled = $this->systemConfigService->get('LnbTrustedShopsRates.config.enableHeader');
if (!$headerEnabled) {
return null;
}
$rates = $this->service->loadAndSaveTrustedShopsRates();
if (!$rates) {
return;
}
$trustedShopsRates = new TrustedShopsRates($rates);
$event->getPagelet()->addExtension('trustedShops', $trustedShopsRates);
}
}