<?php
namespace CioPointsFlow\Subscriber;
use CioBudget\Service\SessionService;
use Shopware\Core\Content\Category\Event\CategoryRouteCacheKeyEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class HomepageNoCacheSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $categoryEntityRepository;
private SessionService $sessionService;
public function __construct(EntityRepositoryInterface $categoryEntityRepository, SessionService $sessionService)
{
$this->categoryEntityRepository = $categoryEntityRepository;
$this->sessionService = $sessionService;
}
public static function getSubscribedEvents()
{
return [
CategoryRouteCacheKeyEvent::class => 'onCategoryRouteCacheKeyEvent'
];
}
public function onCategoryRouteCacheKeyEvent(CategoryRouteCacheKeyEvent $event)
{
$homeCategories = $this->categoryEntityRepository->search((new Criteria())->addFilter(new ContainsFilter('name', 'Home')), $event->getContext()->getContext())->getIds();
if (in_array($event->getNavigationId(), $homeCategories)) {
if ($currentBudgetId = $this->sessionService->getCurrentBudgetId()) {
$event->addPart($currentBudgetId);
}
}
}
}