<?php
namespace Prems\Plugin\PremsIndividualOffer6\Subscriber;
use Prems\Plugin\PremsIndividualOffer6\Core\Offer\Storefront\OfferService;
use Prems\Plugin\PremsIndividualOffer6\PremsIndividualOffer6;
use Shopware\Core\Framework\Routing\KernelListenerPriorities;
use Shopware\Core\PlatformRequest;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class KernelEventsSubscriber implements EventSubscriberInterface
{
/**
* @var OfferService
*/
private OfferService $offerService;
public function __construct(OfferService $offerService)
{
$this->offerService = $offerService;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER => [
['setOfferModeState', KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE_POST],
]
];
}
public function setOfferModeState(ControllerEvent $event): void
{
$request = $event->getRequest();
if (!$request->attributes->has(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT)) {
return;
}
/** @var SalesChannelContext $context */
$context = $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
$offerMode = $this->offerService->getOfferMode($context, $request->getSession());
if ($offerMode) {
$context->addState(PremsIndividualOffer6::OFFER_MODE_STATE);
} else {
$context->removeState(PremsIndividualOffer6::OFFER_MODE_STATE);
}
}
}