custom/plugins/b2bsellerscore/addons/B2bOffer/Subscriber/B2bPlatformSubscriber.php line 83

Open in your IDE?
  1. <?php
  2. namespace B2bOffer\Subscriber;
  3. use B2bOffer\Components\Offer\OfferEntity;
  4. use B2bSellersCore\Components\B2bPlatform\Exception\B2bPlatformContextException;
  5. use B2bSellersCore\Components\B2bPlatform\Traits\B2bContextTrait;
  6. use B2bSellersCore\Components\Employee\Exception\InvalidCustomerException;
  7. use B2bSellersCore\Components\Employee\Service\EmployeeService;
  8. use B2bSellersCore\Components\Employee\Service\EmployeeServiceInterface;
  9. use B2bSellersCore\Components\System\SalesChannel\Events\B2bPlatformConfigAddedEvent;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. use Shopware\Storefront\Framework\Routing\StorefrontResponse;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\RedirectResponse;
  18. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. use Symfony\Component\Routing\RouterInterface;
  21. class B2bPlatformSubscriber implements EventSubscriberInterface
  22. {
  23.     use B2bContextTrait;
  24.     private SystemConfigService $configService;
  25.     private EmployeeService $employeeService;
  26.     private EntityRepositoryInterface $offerRepository;
  27.     private RouterInterface $router;
  28.     public function __construct(
  29.         SystemConfigService       $configService,
  30.         EmployeeServiceInterface  $employeeService,
  31.         EntityRepositoryInterface $offerRepository,
  32.         RouterInterface           $router
  33.     )
  34.     {
  35.         $this->configService $configService;
  36.         $this->employeeService $employeeService;
  37.         $this->offerRepository $offerRepository;
  38.         $this->router $router;
  39.     }
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             B2bPlatformConfigAddedEvent::class => 'addB2bOfferConfig',
  44.             KernelEvents::RESPONSE => 'switchCustomerOnOfferDetail'
  45.         ];
  46.     }
  47.     public function addB2bOfferConfig(B2bPlatformConfigAddedEvent $event)
  48.     {
  49.         $config $event->getB2bPlatformContext()->getConfig();
  50.         $config['b2bOffer'] = [];
  51.         if (!empty($this->configService->get('B2bOffer.config'))) {
  52.             $b2bOfferConfig $this->configService->get('B2bOffer.config');
  53.             if (isset($b2bOfferConfig['customArticleId'])) {
  54.                 $config['b2bOffer']['customArticleId'] = $b2bOfferConfig['customArticleId'];
  55.             }
  56.             if (isset($b2bOfferConfig['defaultOpenStatusId'])) {
  57.                 $config['b2bOffer']['defaultOpenStatusId'] = $b2bOfferConfig['defaultOpenStatusId'];
  58.             }
  59.             if (isset($b2bOfferConfig['defaultCartStatusId'])) {
  60.                 $config['b2bOffer']['defaultCartStatusId'] = $b2bOfferConfig['defaultCartStatusId'];
  61.             }
  62.             if (isset($b2bOfferConfig['defaultClosedStatusId'])) {
  63.                 $config['b2bOffer']['defaultClosedStatusId'] = $b2bOfferConfig['defaultClosedStatusId'];
  64.             }
  65.         }
  66.         $event->getB2bPlatformContext()->setConfig($config);
  67.     }
  68.     public function switchCustomerOnOfferDetail(ResponseEvent $event)
  69.     {
  70.         $response $event->getResponse();
  71.         if (!$response instanceof StorefrontResponse) {
  72.             return;
  73.         }
  74.         $request $event->getRequest();
  75.         if ($request->attributes->get('_route') !== 'frontend.b2b_platform.path'
  76.             || strpos($request->attributes->get('path'), 'offer')
  77.             && !$request->query->has('useCustomer')) {
  78.             return;
  79.         }
  80.         $offerId explode("offer/detail/"$request->getRequestUri());
  81.         if (!isset($offerId[1])){
  82.             return;
  83.         }
  84.         $offer $this->getOfferById($offerId[1], $response->getContext()->getContext());
  85.         if (!$offer) {
  86.             return;
  87.         }
  88.         if (!$this->shouldSwitchCustomer($offer$response->getContext())) {
  89.             return;
  90.         }
  91.         try {
  92.             $this->employeeService->changeCustomerLogin(
  93.                 $offer->getOfferCustomer()->getCustomerId(),
  94.                 $response->getContext()
  95.             );
  96.         } catch (InvalidCustomerException|B2bPlatformContextException $e) {
  97.             $event->setResponse(
  98.                 new RedirectResponse(
  99.                     $this->router->generate('frontend.b2b_platform.path', [
  100.                         'path' => 'offer'
  101.                     ]), 301
  102.                 )
  103.             );
  104.             return;
  105.         }
  106.         $event->setResponse(
  107.             new RedirectResponse(
  108.                 $this->router->generate('frontend.b2b_platform.path', [
  109.                     'path' => 'offer/detail/' $offerId[1]
  110.                 ]), 301
  111.             )
  112.         );
  113.     }
  114.     private function shouldSwitchCustomer(OfferEntity $offerSalesChannelContext $context): bool
  115.     {
  116.         if (empty($offer->getOfferCustomer()->getCustomerId())) {
  117.             return false;
  118.         }
  119.         if ($offer->getOfferCustomer()->getCustomerId() === $context->getCustomer()->getId()) {
  120.             return false;
  121.         }
  122.         $assignedCustomers $this->getEmployee($context)->getCustomers();
  123.         $customer $assignedCustomers->filterByProperty('customerId'$offer->getOfferCustomer()->getCustomerId());
  124.         if (empty($customer) || $customer->count() < 1) {
  125.             return false;
  126.         }
  127.         return true;
  128.     }
  129.     private function getOfferById(string $offerIdContext $context): ?OfferEntity
  130.     {
  131.         $criteria = new Criteria([$offerId]);
  132.         $criteria->addAssociation('offerCustomer');
  133.         return $this->offerRepository->search($criteria$context)->get($offerId);
  134.     }
  135. }