<?phpnamespace App\Context;use Sylius\Component\Channel\Context\ChannelContextInterface;use Sylius\Component\Channel\Context\ChannelNotFoundException;use Sylius\Component\Channel\Model\ChannelInterface;use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\RequestStack;use Webmozart\Assert\Assert;use Symfony\Component\HttpFoundation\Session\SessionInterface;class RequestQueryChannelContext implements ChannelContextInterface{ private $channelRepository; private $requestStack; private $session; public function __construct(ChannelRepositoryInterface $channelRepository, RequestStack $requestStack, SessionInterface $session) { $this->channelRepository = $channelRepository; $this->requestStack = $requestStack; $this->session = $session; } public function getChannel(): ChannelInterface { $request = $this->requestStack->getMasterRequest(); if ($request === null) { $channels = $this->channelRepository->findAll(); return $channels[0]; } Assert::notNull($request); if (!$this->isPathValid($request)) { throw new ChannelNotFoundException('Channel not found!'); } try { $channelCode = $request->query->get('channelCode'); if (isset($channelCode) && !empty($channelCode)) { // stores an attribute in the session for later reuse $this->session->set('channel', $channelCode); } else { $channelCode = $this->session->get('channel'); } if ( empty($channelCode) || is_null($channelCode)) { // Fetch channels /** @var ChannelRepository $channelRepo */ $channels = $this->channelRepository->findAll(); $availableChannels = array(); /** @var Channel $channel */ foreach ($channels as $channel) { if ($channel->isEnabled()) $availableChannels[$channel->getCode()] = $channel; } //Par defaut channel = mons (si activé) if (array_key_exists("Mons", $availableChannels)) $channelCode = "mons"; else $channelCode = "collect"; $this->session->set('channel', $channelCode); } Assert::notNull($channelCode); $channel = $this->channelRepository->findOneByCode($channelCode); if (!$channel instanceof ChannelInterface) { throw new \Sylius\ShopApiPlugin\Exception\ChannelNotFoundException('Channel Not Found!'); } return $channel; } catch (\Sylius\ShopApiPlugin\Exception\ChannelNotFoundException $e) { throw new \Sylius\ShopApiPlugin\Exception\ChannelNotFoundException($e->getMessage()); } catch (\Exception $e) { throw new ChannelNotFoundException('Channel not found!'); } } private function isPathValid(Request $request): bool { return true; $path = $request->getPathInfo(); if (strpos($path, 'shop-api') !== false) { return true; } return false; }}