custom/plugins/PremsIndividualOffer6/src/Subscriber/KernelEventsSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace Prems\Plugin\PremsIndividualOffer6\Subscriber;
  3. use Prems\Plugin\PremsIndividualOffer6\Core\Offer\Storefront\OfferService;
  4. use Prems\Plugin\PremsIndividualOffer6\PremsIndividualOffer6;
  5. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  6. use Shopware\Core\PlatformRequest;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. class KernelEventsSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var OfferService
  15.      */
  16.     private OfferService $offerService;
  17.     public function __construct(OfferService $offerService)
  18.     {
  19.         $this->offerService $offerService;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             KernelEvents::CONTROLLER => [
  25.                 ['setOfferModeState'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE_POST],
  26.             ]
  27.         ];
  28.     }
  29.     public function setOfferModeState(ControllerEvent $event): void
  30.     {
  31.         $request $event->getRequest();
  32.         if (!$request->attributes->has(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT)) {
  33.             return;
  34.         }
  35.         /** @var SalesChannelContext $context */
  36.         $context $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  37.         $offerMode $this->offerService->getOfferMode($context$request->getSession());
  38.         if ($offerMode) {
  39.            $context->addState(PremsIndividualOffer6::OFFER_MODE_STATE);
  40.         } else {
  41.             $context->removeState(PremsIndividualOffer6::OFFER_MODE_STATE);
  42.         }
  43.     }
  44. }