<?php declare(strict_types=1);
namespace Swkweb\Infobar\Storefront\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
use Swkweb\Infobar\Core\Content\InfobarEntry\InfobarEntryCollection;
use Swkweb\Infobar\Struct\Infobar;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class FooterSubscriber implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var EntityRepositoryInterface
*/
private $infobarEntryRepository;
public function __construct(
SystemConfigService $systemConfigService,
EntityRepositoryInterface $infobarEntryRepository
) {
$this->systemConfigService = $systemConfigService;
$this->infobarEntryRepository = $infobarEntryRepository;
}
public static function getSubscribedEvents(): array
{
return [
FooterPageletLoadedEvent::class => 'onFooterPageletLoaded',
];
}
public function onFooterPageletLoaded(FooterPageletLoadedEvent $event): void
{
$criteria = (new Criteria())
->addFilter(
new EqualsFilter(
'salesChannelId',
$event->getSalesChannelContext()->getSalesChannel()->getId()
)
)
->addSorting(new FieldSorting('position'))
;
$result = $this->infobarEntryRepository->search($criteria, $event->getContext());
if (!$result->getTotal()) {
return;
}
/** @var InfobarEntryCollection $entries */
$entries = $result->getEntities();
$infobar = new Infobar();
$infobar->setPosition($this->systemConfigService->getString('SwkwebInfobar.config.position'));
$infobar->setMobile($this->systemConfigService->getBool('SwkwebInfobar.config.mobile'));
$infobar->setMoreHover($this->systemConfigService->getBool('SwkwebInfobar.config.moreHover'));
$infobar->setClose($this->systemConfigService->getBool('SwkwebInfobar.config.close'));
$infobar->setEntries($entries);
$event->getPagelet()->addExtension('swkwebInfobar', $infobar);
}
}