src/Context/RequestQueryChannelContext.php line 79

Open in your IDE?
  1. <?php
  2. namespace App\Context;
  3. use Sylius\Component\Channel\Context\ChannelContextInterface;
  4. use Sylius\Component\Channel\Context\ChannelNotFoundException;
  5. use Sylius\Component\Channel\Model\ChannelInterface;
  6. use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Webmozart\Assert\Assert;
  10. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  11. class RequestQueryChannelContext implements ChannelContextInterface
  12. {
  13.     private $channelRepository;
  14.     private $requestStack;
  15.     private $session;
  16.     public function __construct(ChannelRepositoryInterface $channelRepositoryRequestStack $requestStackSessionInterface $session)
  17.     {
  18.         $this->channelRepository $channelRepository;
  19.         $this->requestStack $requestStack;
  20.         $this->session $session;
  21.     }
  22.     public function getChannel(): ChannelInterface
  23.     {
  24.         $request $this->requestStack->getMasterRequest();
  25.         if ($request === null) {
  26.             $channels =  $this->channelRepository->findAll();
  27.             return $channels[0];
  28.         }
  29.         Assert::notNull($request);
  30.         if (!$this->isPathValid($request)) {
  31.             throw new ChannelNotFoundException('Channel not found!');
  32.         }
  33.         try {
  34.             $channelCode $request->query->get('channelCode');
  35.             if (isset($channelCode) && !empty($channelCode))
  36.             {
  37.                     // stores an attribute in the session for later reuse
  38.                     $this->session->set('channel'$channelCode);
  39.             }
  40.             else
  41.             {
  42.                  $channelCode $this->session->get('channel');
  43.             }
  44.             if ( empty($channelCode) || is_null($channelCode))
  45.             {
  46.                 // Fetch channels
  47.                 /** @var ChannelRepository $channelRepo */
  48.                 $channels =  $this->channelRepository->findAll();
  49.                 $availableChannels = array();
  50.                 /** @var Channel $channel */
  51.                 foreach ($channels as $channel) {
  52.                     if ($channel->isEnabled())
  53.                         $availableChannels[$channel->getCode()] = $channel;
  54.                 }
  55.                 //Par defaut channel = mons (si activĂ©)
  56.                 if (array_key_exists("Mons"$availableChannels))
  57.                     $channelCode "mons";
  58.                 else
  59.                     $channelCode "collect";
  60.                 $this->session->set('channel',  $channelCode);
  61.             }
  62.             Assert::notNull($channelCode);
  63.             $channel $this->channelRepository->findOneByCode($channelCode);
  64.             if (!$channel instanceof ChannelInterface) {
  65.                 throw new \Sylius\ShopApiPlugin\Exception\ChannelNotFoundException('Channel Not Found!');
  66.             }
  67.             return $channel;
  68.         } catch (\Sylius\ShopApiPlugin\Exception\ChannelNotFoundException $e) {
  69.             throw new \Sylius\ShopApiPlugin\Exception\ChannelNotFoundException($e->getMessage());
  70.         } catch (\Exception $e) {
  71.             throw new ChannelNotFoundException('Channel not found!');
  72.         }
  73.     }
  74.     private function isPathValid(Request $request): bool
  75.     {
  76.         return true;
  77.         $path $request->getPathInfo();
  78.         if (strpos($path'shop-api') !== false) {
  79.             return true;
  80.         }
  81.         return false;
  82.     }
  83. }