vendor/sylius/sylius/src/Sylius/Bundle/ChannelBundle/Collector/ChannelCollector.php line 60

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ChannelBundle\Collector;
  12. use Sylius\Component\Channel\Context\ChannelContextInterface;
  13. use Sylius\Component\Channel\Context\ChannelNotFoundException;
  14. use Sylius\Component\Channel\Model\ChannelInterface;
  15. use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  19. final class ChannelCollector extends DataCollector
  20. {
  21.     private ChannelContextInterface $channelContext;
  22.     public function __construct(
  23.         ChannelRepositoryInterface $channelRepository,
  24.         ChannelContextInterface $channelContext,
  25.         bool $channelChangeSupport false
  26.     ) {
  27.         $this->channelContext $channelContext;
  28.         $this->data = [
  29.             'channel' => null,
  30.             'channels' => array_map([$this'pluckChannel'], $channelRepository->findAll()),
  31.             'channel_change_support' => $channelChangeSupport,
  32.         ];
  33.     }
  34.     public function getChannel(): ?array
  35.     {
  36.         return $this->data['channel'];
  37.     }
  38.     /**
  39.      * @return iterable|ChannelInterface[]
  40.      */
  41.     public function getChannels(): iterable
  42.     {
  43.         return $this->data['channels'];
  44.     }
  45.     public function isChannelChangeSupported(): bool
  46.     {
  47.         return $this->data['channel_change_support'];
  48.     }
  49.     public function collect(Request $requestResponse $response\Throwable $exception null): void
  50.     {
  51.         try {
  52.             $this->data['channel'] = $this->pluckChannel($this->channelContext->getChannel());
  53.         } catch (ChannelNotFoundException $exception) {
  54.         }
  55.     }
  56.     public function reset(): void
  57.     {
  58.         $this->data['channel'] = null;
  59.     }
  60.     public function getName(): string
  61.     {
  62.         return 'sylius.channel_collector';
  63.     }
  64.     private function pluckChannel(ChannelInterface $channel): array
  65.     {
  66.         return [
  67.             'name' => $channel->getName(),
  68.             'hostname' => $channel->getHostname(),
  69.             'code' => $channel->getCode(),
  70.         ];
  71.     }
  72. }