src/API/EventSubscriber/Storefront/ProfileDetectionEventSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace XCart\API\EventSubscriber\Storefront;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\Security\Core\Security;
  11. use XLite\Model\Profile as ProfileModel;
  12. use XLite\Model\Repo\Profile;
  13. class ProfileDetectionEventSubscriber implements EventSubscriberInterface
  14. {
  15.     public function __construct(
  16.         private Security $security,
  17.         private Profile $profileRepository,
  18.     ) {
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             KernelEvents::REQUEST => [
  24.                 ['process'7],
  25.             ],
  26.         ];
  27.     }
  28.     public function process(RequestEvent $event): void
  29.     {
  30.         if (!$event->isMainRequest() || $event->getRequest()->getMethod() === 'OPTIONS') {
  31.             return;
  32.         }
  33.         if (!preg_match('/api\/storefront\//S'$event->getRequest()->getPathInfo())) {
  34.             return;
  35.         }
  36.         $user $this->security->getUser();
  37.         if (!$user || !$user->getUserIdentifier()) {
  38.             return;
  39.         }
  40.         /** @var ProfileModel $profile */
  41.         $profile $this->profileRepository->findOneBy(['login' => $user->getUserIdentifier()]);
  42.         if (!$profile) {
  43.             return;
  44.         }
  45.         $event->getRequest()->attributes->set('_profile_id'$profile->getProfileId());
  46.         $event->getRequest()->attributes->set('_profile'$profile);
  47.     }
  48. }