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

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  * (c) Paweł Jędrzejewski
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. declare(strict_types=1);
  9. namespace Sylius\ShopApiPlugin\Http;
  10. use Sylius\ShopApiPlugin\Checker\ChannelExistenceCheckerInterface;
  11. use Sylius\ShopApiPlugin\Exception\ChannelNotFoundException;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. final class RequestChannelEnsurer implements EventSubscriberInterface
  17. {
  18.     /** @var ChannelExistenceCheckerInterface */
  19.     private $channelExistenceChecker;
  20.     public function __construct(ChannelExistenceCheckerInterface $channelExistenceChecker)
  21.     {
  22.         $this->channelExistenceChecker $channelExistenceChecker;
  23.     }
  24.     public function checkChannelCode(ControllerEvent $event): void
  25.     {
  26.         $requestAttributes $event->getRequest()->attributes;
  27.         if (!$requestAttributes->has('channelCode')) {
  28.             return;
  29.         }
  30.         try {
  31.             $this->channelExistenceChecker->withCode($requestAttributes->get('channelCode'));
  32.         } catch (ChannelNotFoundException $exception) {
  33.             throw new NotFoundHttpException($exception->getMessage());
  34.         }
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             KernelEvents::CONTROLLER => 'checkChannelCode',
  40.         ];
  41.     }
  42. }