vendor/sylius/shop-api-plugin/src/Http/RequestChannelEnsurer.php line 24

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sylius\ShopApiPlugin\Http;
  4. use Sylius\ShopApiPlugin\Checker\ChannelExistenceCheckerInterface;
  5. use Sylius\ShopApiPlugin\Exception\ChannelNotFoundException;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. final class RequestChannelEnsurer implements EventSubscriberInterface
  11. {
  12.     /** @var ChannelExistenceCheckerInterface */
  13.     private $channelExistenceChecker;
  14.     public function __construct(ChannelExistenceCheckerInterface $channelExistenceChecker)
  15.     {
  16.         $this->channelExistenceChecker $channelExistenceChecker;
  17.     }
  18.     public function checkChannelCode(FilterControllerEvent $event): void
  19.     {
  20.         $requestAttributes $event->getRequest()->attributes;
  21.         if (!$requestAttributes->has('channelCode')) {
  22.             return;
  23.         }
  24.         try {
  25.             $this->channelExistenceChecker->withCode($requestAttributes->get('channelCode'));
  26.         } catch (ChannelNotFoundException $exception) {
  27.             throw new NotFoundHttpException($exception->getMessage());
  28.         }
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             KernelEvents::CONTROLLER => 'checkChannelCode',
  34.         ];
  35.     }
  36. }