custom/plugins/b2bsellerscore/src/Components/B2bPlatform/Subscriber/StorefrontSubscriber.php line 90

Open in your IDE?
  1. <?php
  2. namespace B2bSellersCore\Components\B2bPlatform\Subscriber;
  3. use B2bSellersCore\Components\B2bPlatform\Traits\B2bContextTrait;
  4. use B2bSellersCore\Components\Employee\Aggregate\EmployeeCustomer\EmployeeCustomerCollection;
  5. use B2bSellersCore\Storefront\Page\B2bSelectEmployeeAdministrator\B2bSelectEmployeeAdministratorPage;
  6. use LogicException;
  7. use Shopware\Core\Framework\Adapter\Translation\AbstractTranslator;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\Struct\ArrayStruct;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Shopware\Storefront\Event\StorefrontRenderEvent;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  17. class StorefrontSubscriber implements EventSubscriberInterface
  18. {
  19.     use B2bContextTrait;
  20.     private const B2B_PLATFORM_CONFIG_BLACKLIST = ['username''password''accessKey''relativeDocumentPath'];
  21.     private SystemConfigService $configService;
  22.     private EntityRepositoryInterface $employeeCustomerRepository;
  23.     private ContainerInterface $container;
  24.     private AbstractTranslator $translator;
  25.     public function __construct(
  26.         SystemConfigService       $configService,
  27.         EntityRepositoryInterface $employeeCustomerRepository,
  28.         ContainerInterface        $container,
  29.         AbstractTranslator        $translator
  30.     )
  31.     {
  32.         $this->configService $configService;
  33.         $this->employeeCustomerRepository $employeeCustomerRepository;
  34.         $this->container $container;
  35.         $this->translator $translator;
  36.     }
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             StorefrontRenderEvent::class => [
  41.                 ['addB2bPlatformConfig'],
  42.                 ['checkForEmployeeAdmins']
  43.             ]
  44.         ];
  45.     }
  46.     public function addB2bPlatformConfig(StorefrontRenderEvent $event)
  47.     {
  48.         $context $event->getSalesChannelContext();
  49.         $config = [];
  50.         if (!empty($this->configService->get('B2bSellersCore.config'$context->getSalesChannelId()))) {
  51.             $config array_diff_key(
  52.                 $this->configService->get('B2bSellersCore.config'$context->getSalesChannelId()),
  53.                 array_flip(self::B2B_PLATFORM_CONFIG_BLACKLIST)
  54.             );
  55.         }
  56.         $event->setParameter('b2bPlatformConfig', new ArrayStruct($config));
  57.         if (!$context->getCustomer()) {
  58.             $event->setParameter('isSalesRepresentative'false);
  59.             $event->setParameter('loggedInAsCustomer'false);
  60.             $event->setParameter('isEmployee'false);
  61.             $event->setParameter('employee'null);
  62.             $event->setParameter('contactPerson'null);
  63.             return;
  64.         }
  65.         if (!$this->hasB2bPlatformContext($context)) {
  66.             return;
  67.         }
  68.         $b2bPlatformContext $this->getB2bPlatformContext($context);
  69.         $event->setParameter('isSalesRepresentative'$b2bPlatformContext->isSalesRepresentative());
  70.         $event->setParameter('loggedInAsCustomer'$this->isSalesRepresentativeLoggedInAsCustomer($context));
  71.         $event->setParameter('isEmployee'$b2bPlatformContext->isEmployee());
  72.         $event->setParameter('employee'$b2bPlatformContext->getEmployee());
  73.         $event->setParameter('contactPerson'$b2bPlatformContext->getContactPerson());
  74.     }
  75.     public function checkForEmployeeAdmins(StorefrontRenderEvent $event): void
  76.     {
  77.         $context $event->getSalesChannelContext();
  78.         if (!$this->hasB2bPlatformContext($context)) {
  79.             return;
  80.         }
  81.         if (isset($event->getParameters()['page']) && $event->getParameters()['page'] instanceof B2bSelectEmployeeAdministratorPage) {
  82.             return;
  83.         }
  84.         $criteria = new Criteria();
  85.         $criteria->addFilter(new EqualsFilter('customerId'$context->getCustomer()->getId()));
  86.         /** @var EmployeeCustomerCollection $employeeCustomers */
  87.         $employeeCustomers $this->employeeCustomerRepository->search($criteria$context->getContext())->getEntities();
  88.         if ($employeeCustomers->count() > && $employeeCustomers->getAdmins()->count() <= 0) {
  89.             $this->container->get('session')->getFlashBag()->clear();
  90.             if ($this->getEmployee($context) !== null) {
  91.                 if ($this->getEmployee($context)->getEmail() == $context->getCustomer()->getEmail()) {
  92.                     $this->addFlash('danger'$this->translator->trans('b2bPlatform.employee.messages.noEmployeeAdminMailSameCustomerError'));
  93.                     return;
  94.                 }
  95.             }
  96.             $this->addFlash('danger'$this->translator->trans('b2bPlatform.employee.messages.noEmployeeAdminError', ['%mail%' => $context->getCustomer()->getEmail()]));
  97.         }
  98.     }
  99.     /**
  100.      * Adds a flash message to the current session for type.
  101.      *
  102.      * @throws LogicException
  103.      */
  104.     protected function addFlash(string $type$message): void
  105.     {
  106.         try {
  107.             $this->container->get('session')->getFlashBag()->add($type$message);
  108.         } catch (SessionNotFoundException $e) {
  109.             throw new LogicException('You can not use the addFlash method if sessions are disabled. Enable them in "config/packages/framework.yaml".'0$e);
  110.         }
  111.     }
  112. }