custom/plugins/SelfdelveCms/src/Subscriber/CustomerLoadedSubscriber.php line 51

Open in your IDE?
  1. <?php
  2. namespace Selfdelve\Cms\Subscriber;
  3. use Shopware\Core\Checkout\Customer\CustomerEvents;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Core\Checkout\Customer\CustomerEntity;
  6. use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupEntity;
  7. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  11. class CustomerLoadedSubscriber implements EventSubscriberInterface
  12. {
  13.     const UNVERIFIED_PATTERN '***%s***UNVERIFIED***';
  14.     private EntityRepositoryInterface $customerRepository;
  15.     private EntityRepositoryInterface $customerGroupRepository;
  16.     /**
  17.      * @param EntityRepositoryInterface $customerRepository
  18.      * @param EntityRepositoryInterface $customerGroupRepository
  19.      */
  20.     public function __construct(
  21.         EntityRepositoryInterface $customerRepository,
  22.         EntityRepositoryInterface $customerGroupRepository
  23.     )
  24.     {
  25.         $this->customerRepository $customerRepository;
  26.         $this->customerGroupRepository $customerGroupRepository;
  27.     }
  28.     /**
  29.      * @return array
  30.      */
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             CustomerEvents::CUSTOMER_LOADED_EVENT => 'onCustomerLoaded',
  35.         ];
  36.     }
  37.     /**
  38.      * If a business user requested membership in a business customer group and
  39.      * the request is still pending, then mask the given vat ids in order to avoid illegitimate orders.
  40.      *
  41.      * @param EntityLoadedEvent $event
  42.      * @return void
  43.      */
  44.     public function onCustomerLoaded(EntityLoadedEvent $event)
  45.     {
  46.         // Ignore in backend
  47.         if ($event->getContext()->getSource() instanceof AdminApiSource) {
  48.             return;
  49.         }
  50.         if (=== count($event->getEntities())) {
  51.             return;
  52.         }
  53.         foreach ($event->getEntities() as $entity) {
  54.             if (!$entity instanceof CustomerEntity || '' === (string) $entity->getRequestedGroupId()) {
  55.                 continue;
  56.             }
  57.             /** @var CustomerGroupEntity $customerGroup */
  58.             $customerGroup $this->customerGroupRepository->search(
  59.                 new Criteria([$entity->getRequestedGroupId()]),
  60.                 $event->getContext()
  61.             )->first();
  62.             if (!$customerGroup instanceof CustomerGroupEntity || false === $customerGroup->getRegistrationOnlyCompanyRegistration()) {
  63.                 continue;
  64.             }
  65.             // Business customer group requested
  66.             $vatIds $entity->getVatIds();
  67.             foreach ($vatIds as &$vatId) {
  68.                 $vatId sprintf(self::UNVERIFIED_PATTERN$vatId);
  69.             }
  70.             $entity->setVatIds($vatIds);
  71.         }
  72.     }
  73. }