custom/static-plugins/SamsonCustomer/src/Subscriber/PageSubscriber.php line 53

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Samson\Subscriber;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\Struct\ArrayStruct;
  8. use Shopware\Core\Framework\Struct\StructCollection;
  9. use Shopware\Core\System\SalesChannel\Aggregate\SalesChannelDomain\SalesChannelDomainEntity;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Routing\RouterInterface;
  15. /***
  16.  *
  17.  * This file is part of the "SAMSON Shop" project.
  18.  *
  19.  * For the full copyright and license information, please read the
  20.  * LICENSE.txt file that was distributed with this source code.
  21.  *
  22.  *  (c) 2022
  23.  *
  24.  ***/
  25. class PageSubscriber implements EventSubscriberInterface
  26. {
  27.     private SystemConfigService $systemConfigService;
  28.     private EntityRepositoryInterface $salesChannelDomainRepository;
  29.     private RouterInterface $router;
  30.     public function __construct(SystemConfigService         $systemConfigService,
  31.                                 EntityRepositoryInterface   $salesChannelDomainRepository,
  32.                                 RouterInterface             $router
  33.     )
  34.     {
  35.         $this->systemConfigService $systemConfigService;
  36.         $this->salesChannelDomainRepository $salesChannelDomainRepository;
  37.         $this->router $router;
  38.     }
  39.     /** {@inheritDoc} */
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             GenericPageLoadedEvent::class => 'onPageLoaded'
  44.         ];
  45.     }
  46.     public function onPageLoaded(GenericPageLoadedEvent $event)
  47.     {
  48.         $keys = ['first''second''third'];
  49.         $result = [];
  50.         foreach ($keys as $key) {
  51.             $result[] = $this->loadConfig($key$event->getSalesChannelContext());
  52.         }
  53.         $event->getPage()->addExtension('salesChannels', new StructCollection(array_filter($result)));
  54.     }
  55.     private function loadConfig(string $keySalesChannelContext $salesChannelContext): ?ArrayStruct
  56.     {
  57.         $context        $salesChannelContext->getContext();
  58.         $salesChannelId $salesChannelContext->getSalesChannelId();
  59.         $id     $this->systemConfigService->get("SamsonCustomer.config.{$key}SalesChannel"$salesChannelId);
  60.         $route  $this->systemConfigService->get("SamsonCustomer.config.{$key}SalesChannelRouteName"$salesChannelId);
  61.         if (!$id) {
  62.             return null;
  63.         }
  64.         $url            $this->generateUrl($id$route$context);
  65.         $color          $this->systemConfigService->get('SamsonCustomer.config.samsonPrimaryColor'$id);
  66.         $contrastColor  $this->systemConfigService->get('SamsonCustomer.config.samsonPrimaryContrastColor'$id);
  67.         if (!$url || !$color) {
  68.             return null;
  69.         }
  70.         return new ArrayStruct([
  71.             'id'            => $id,
  72.             'color'         => $color,
  73.             'contrastColor' => $contrastColor,
  74.             'label'         => "samson.sales-channel.{$key}-sales-channel",
  75.             'url'           => $url,
  76.         ]);
  77.     }
  78.     private function generateUrl(string $salesChannelId, ?string $routeContext $context): ?string
  79.     {
  80.         $criteria = (new Criteria())
  81.             ->addFilter(new EqualsFilter('salesChannelId'$salesChannelId))
  82.             ->addFilter(new EqualsFilter('languageId'$context->getLanguageId()))
  83.         ;
  84.         /* @var SalesChannelDomainEntity $salesChannelDomain */
  85.         $salesChannelDomain $this->salesChannelDomainRepository->search($criteria$context)->first();
  86.         if (!$salesChannelDomain) {
  87.             return null;
  88.         }
  89.         $context $this->router->getContext();
  90.         $oldBaseUrl $context->getBaseUrl();
  91.         $newBaseUrl $salesChannelDomain->getUrl();
  92.         $route $route ?? 'frontend.home.page';
  93.         $context->setBaseUrl($newBaseUrl);
  94.         $url $this->router->generate($route);
  95.         $context->setBaseUrl($oldBaseUrl);
  96.         return $url;
  97.     }
  98. }