custom/static-plugins/AbasConnector/src/Subscribers/Customer/CustomerRoleSubscriber.php line 89

Open in your IDE?
  1. <?php
  2. namespace Abas\AbasConnector\Subscribers\Customer;
  3. use Abas\AbasConnector\Service\Core\Content\CustomerRole\CustomerRoleEntity;
  4. use Abas\AbasConnector\Service\Core\Content\CustomerRole\CustomerRoleRelationEntity;
  5. use Abas\AbasConnector\Service\Core\Enums\CustomerRole;
  6. use Abas\AbasConnector\Service\Core\Enums\Duration;
  7. use Abas\AbasConnector\Service\Log\AbasLogger;
  8. use Abas\AbasConnector\Service\Utils\Benchmark\Benchmark;
  9. use Shopware\Core\Checkout\Customer\CustomerEntity;
  10. use Shopware\Core\Checkout\Customer\CustomerEvents;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  16. use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Contracts\Cache\CacheInterface;
  19. use Symfony\Contracts\Cache\ItemInterface;
  20. class CustomerRoleSubscriber implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var string
  24.      */
  25.     protected const CACHE_PREFIX 'CustomerRole_';
  26.     /**
  27.      * @var EntityRepositoryInterface
  28.      */
  29.     protected $customerRoleRelationRepository;
  30.     /**
  31.      * @var EntityRepositoryInterface
  32.      */
  33.     protected $customerRoleRepository;
  34.     /**
  35.      * @var CustomerRoleEntity[]
  36.      */
  37.     protected $customerRoleEntities = [];
  38.     /**
  39.      * @var AbasLogger
  40.      */
  41.     protected AbasLogger $logger;
  42.     /**
  43.      * @var CacheInterface
  44.      */
  45.     protected CacheInterface $cache;
  46.     /**
  47.      * @param EntityRepositoryInterface $customerRoleRelationRepository
  48.      * @param EntityRepositoryInterface $customerRoleRepository
  49.      * @param AbasLogger $logger
  50.      * @param CacheInterface $cache
  51.      */
  52.     public function __construct(
  53.         EntityRepositoryInterface $customerRoleRelationRepository,
  54.         EntityRepositoryInterface $customerRoleRepository,
  55.         AbasLogger $logger,
  56.         CacheInterface $cache
  57.     ) {
  58.         $this->customerRoleRelationRepository $customerRoleRelationRepository;
  59.         $this->customerRoleRepository $customerRoleRepository;
  60.         $this->logger $logger;
  61.         $this->cache $cache;
  62.     }
  63.     /**
  64.      * @inheritDoc
  65.      */
  66.     public static function getSubscribedEvents(): array
  67.     {
  68.         return [
  69.             SalesChannelContextResolvedEvent::class => 'assignCustomerRoleToContext',
  70.             CustomerEvents::CUSTOMER_LOADED_EVENT => 'assignCustomerRoleToCustomers'
  71.         ];
  72.     }
  73.     /**
  74.      * @param SalesChannelContextResolvedEvent $event
  75.      *
  76.      * @return void
  77.      */
  78.     public function assignCustomerRoleToContext(SalesChannelContextResolvedEvent $event): void
  79.     {
  80.         $salesChannelContext $event->getSalesChannelContext();
  81.         $customer $salesChannelContext->getCustomer();
  82.         if ($customer !== null) {
  83.             $customerRole $customer->getExtension('customerRole');
  84.         } else {
  85.             $customerRole $this->getDefaultCustomerRole($salesChannelContext->getContext());
  86.         }
  87.         $salesChannelContext->assign(['customerRole' => $customerRole]);
  88.     }
  89.     /**
  90.      * @param EntityLoadedEvent $event
  91.      *
  92.      * @return void
  93.      */
  94.     public function assignCustomerRoleToCustomers(EntityLoadedEvent $event): void
  95.     {
  96.         $startTime microtime(true);
  97.         $context $event->getContext();
  98.         $customers $event->getEntities();
  99.         if (empty($customers)) {
  100.             return;
  101.         }
  102.         /** @var CustomerEntity $customer */
  103.         foreach ($customers as $customer) {
  104.             $this->assignCustomerRoleToCustomer($customer$context);
  105.         }
  106.     // commented because this floods public/var/log/app.log
  107.         //$this->logger->info('CustomerRoleSubscriber: assigned customer roles for ' . count($customers)
  108.         //    . ' customers. Took ' . Benchmark::getFormattedTime($startTime));
  109.     }
  110.     /**
  111.      * @param CustomerEntity $customer
  112.      * @param Context $context
  113.      *
  114.      * @return void
  115.      */
  116.     private function assignCustomerRoleToCustomer(CustomerEntity $customerContext $context): void
  117.     {
  118.         $customerId $customer->getId();
  119.         $customerRoleRelation $this->getCustomerRoleRelationByCustomer($customerId$context);
  120.         $customer->addExtensions([
  121.             'customerRole' => $this->getCustomerRole($customerId$context$customerRoleRelation),
  122.             'customerRoleRelation' => $customerRoleRelation
  123.         ]);
  124.     }
  125.     /**
  126.      * @param string $customerId
  127.      * @param Context $context
  128.      * @param null|CustomerRoleRelationEntity $customerRoleRelationEntity
  129.      *
  130.      * @return CustomerRoleEntity
  131.      */
  132.     private function getCustomerRole(
  133.         string $customerId,
  134.         Context $context,
  135.         ?CustomerRoleRelationEntity $customerRoleRelationEntity
  136.     ): CustomerRoleEntity {
  137.         // Create new entity and retrieve it.
  138.         if ($customerRoleRelationEntity === null) {
  139.             $this->createCustomerRoleRelation($customerId$context);
  140.             $customerRoleRelationEntity $this->getCustomerRoleRelationByCustomer($customerId$context);
  141.         }
  142.         return $customerRoleRelationEntity->getCustomerRole();
  143.     }
  144.     /**
  145.      * @param string $customerId
  146.      * @param Context $context
  147.      *
  148.      * @return CustomerRoleRelationEntity|null
  149.      */
  150.     private function getCustomerRoleRelationByCustomer(
  151.         string $customerId,
  152.         Context $context
  153.     ): ?CustomerRoleRelationEntity {
  154.         $criteria = new Criteria();
  155.         $criteria->addFilter(new EqualsFilter('customerId'$customerId));
  156.         $criteria->addAssociation('customerRole.permissions');
  157.         $criteria->setLimit(1);
  158.         return $this->customerRoleRelationRepository->search($criteria$context)->first();
  159.     }
  160.     /**
  161.      * Derzeit nicht in Benutzung, da er nicht korrekt cached.
  162.      * @param string $customerId
  163.      * @param Context $context
  164.      *
  165.      * @return CustomerRoleRelationEntity|null
  166.      */
  167.     private function getCachedCustomerRoleRelationByCustomer(
  168.         string $customerId,
  169.         Context $context
  170.     ): ?CustomerRoleRelationEntity {
  171.         $cacheKey self::CACHE_PREFIX $customerId;
  172.         return $this->cache->get(
  173.             $cacheKey,
  174.             function (ItemInterface $item) use ($customerId$context) {
  175.                 $item->expiresAfter(Duration::FOUR_HOURS);
  176.                 return $this->getCustomerRoleRelationByCustomer($customerId$context);
  177.             }
  178.         );
  179.     }
  180.     /**
  181.      * @param string $customerId
  182.      * @param Context $context
  183.      *
  184.      * @return void
  185.      */
  186.     private function createCustomerRoleRelation(string $customerIdContext $context): void
  187.     {
  188.         $this->customerRoleRelationRepository->upsert(
  189.             [
  190.                 [
  191.                     'customerId' => $customerId,
  192.                     'customerRoleId' => $this->getCustomerRoleByKey(CustomerRole::USER$context)->getId()
  193.                 ]
  194.             ],
  195.             $context
  196.         );
  197.     }
  198.     /**
  199.      * @param Context $context
  200.      *
  201.      * @return CustomerRoleEntity
  202.      */
  203.     private function getDefaultCustomerRole(Context $context): CustomerRoleEntity
  204.     {
  205.         return $this->getCustomerRoleByKey(CustomerRole::GUEST$context);
  206.     }
  207.     /**
  208.      * @param string $key
  209.      * @param Context $context
  210.      *
  211.      * @return CustomerRoleEntity
  212.      */
  213.     private function getCustomerRoleByKey(string $keyContext $context): CustomerRoleEntity
  214.     {
  215.         if (empty($this->customerRoleEntities[$key])) {
  216.             $criteria = new Criteria();
  217.             $criteria->addFilter(new EqualsFilter('roleKey'$key));
  218.             $criteria->addAssociation('permissions');
  219.             $criteria->setLimit(1);
  220.             $this->customerRoleEntities[$key] = $this->customerRoleRepository->search($criteria$context)->first();
  221.         }
  222.         return $this->customerRoleEntities[$key];
  223.     }
  224. }