custom/plugins/NetiNextAccessManager/src/Subscriber/Customer.php line 168

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (c) 2020, Net Inventors GmbH
  4.  * @category  Shopware
  5.  * @author    malte
  6.  */
  7. namespace NetInventors\NetiNextAccessManager\Subscriber;
  8. use NetInventors\NetiNextAccessManager\Constants\ConfigConstants;
  9. use NetInventors\NetiNextAccessManager\Events\BusinessEvent\CustomerActivateEvent;
  10. use NetInventors\NetiNextAccessManager\Events\BusinessEvent\CustomerDeactivateEvent;
  11. use NetInventors\NetiNextAccessManager\Extension\Content\Customer\Aggregate\CustomerAttribute\CustomerAttributeEntity;
  12. use NetInventors\NetiNextAccessManager\Service\CustomerService;
  13. use NetInventors\NetiNextAccessManager\Service\PluginConfig;
  14. use Shopware\Core\Checkout\Customer\CustomerEntity;
  15. use Shopware\Core\Framework\Context;
  16. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  19. use Shopware\Core\PlatformRequest;
  20. use Shopware\Core\System\SystemConfig\SystemConfigService;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Shopware\Core\Checkout\Customer\CustomerEvents;
  23. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  24. use Symfony\Component\HttpFoundation\RequestStack;
  25. use Symfony\Component\HttpKernel\Event\RequestEvent;
  26. use Symfony\Component\HttpKernel\KernelEvents;
  27. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  28. class Customer implements EventSubscriberInterface
  29. {
  30.     /**
  31.      * @var bool
  32.      */
  33.     private $isFirstActivation false;
  34.     /**
  35.      * @var RequestStack
  36.      */
  37.     private $requestStack;
  38.     /**
  39.      * @var EntityRepositoryInterface
  40.      */
  41.     private $customerRepository;
  42.     /**
  43.      * @var PluginConfig
  44.      */
  45.     private $pluginConfig;
  46.     /**
  47.      * @var CustomerService
  48.      */
  49.     private $customerService;
  50.     /**
  51.      * @var EventDispatcherInterface
  52.      */
  53.     private $eventDispatcher;
  54.     /**
  55.      * Customer constructor.
  56.      *
  57.      * @param RequestStack              $requestStack
  58.      * @param EntityRepositoryInterface $customerRepository
  59.      * @param PluginConfig              $pluginConfig
  60.      * @param CustomerService           $customerService
  61.      * @param EventDispatcherInterface  $eventDispatcher
  62.      */
  63.     public function __construct(
  64.         RequestStack $requestStack,
  65.         EntityRepositoryInterface $customerRepository,
  66.         PluginConfig $pluginConfig,
  67.         CustomerService $customerService,
  68.         EventDispatcherInterface $eventDispatcher
  69.     ) {
  70.         $this->requestStack        $requestStack;
  71.         $this->customerRepository  $customerRepository;
  72.         $this->pluginConfig        $pluginConfig;
  73.         $this->customerService     $customerService;
  74.         $this->eventDispatcher     $eventDispatcher;
  75.     }
  76.     public static function getSubscribedEvents(): array
  77.     {
  78.         return [
  79.             CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'onCustomerWritten',
  80.             KernelEvents::REQUEST                  => 'onKernelRequest',
  81.             CustomerEvents::CUSTOMER_LOADED_EVENT  => 'onCustomerLoaded',
  82.         ];
  83.     }
  84.     public function onCustomerLoaded(EntityLoadedEvent $event): void
  85.     {
  86.         $entities $event->getEntities();
  87.         /** @var CustomerEntity $entity */
  88.         foreach ($entities as $entity) {
  89.             if (null === $entity->getExtension('netiAccessManagerCustomerAttribute')) {
  90.                 $this->customerService->setActiveAttribute($entity->getId(), true$event->getContext());
  91.             }
  92.         }
  93.     }
  94.     public function onCustomerWritten(EntityWrittenEvent $event): void
  95.     {
  96.         $masterRequest $this->requestStack->getMasterRequest();
  97.         if (null === $masterRequest) {
  98.             return;
  99.         }
  100.         $attribute $masterRequest->get('netiAccessManagerCustomerAttribute');
  101.         if (!
  102.             (
  103.                 \is_array($attribute)
  104.                 && isset($attribute['activated'])
  105.             )
  106.         ) {
  107.             return;
  108.         }
  109.         $customerId $attribute['id'];
  110.         $customer   $this->customerRepository->search(new Criteria([ $customerId ]), $event->getContext())->first();
  111.         if (!$customer instanceof CustomerEntity) {
  112.             return;
  113.         }
  114.         $pluginConfig $this->pluginConfig->loadConfigOfSalesChannel($customer->getSalesChannelId());
  115.         if (!$pluginConfig->isActive()) {
  116.             return;
  117.         }
  118.         $isMailSendingOnChangeActivation =
  119.             $pluginConfig->getStatusChangeNotification() === ConfigConstants::STATUS_CHANGE_NOTIFICATION_CHANGE;
  120.         $context                         $event->getContext();
  121.         $customer                        $this->customerService->getCustomerWithSalutation($customerId$context);
  122.         if (!$customer instanceof CustomerEntity) {
  123.             throw new \RuntimeException('Customer was not found by id '$customerId);
  124.         }
  125.         $customerContext = new Context(
  126.             $context->getSource(),
  127.             $context->getRuleIds(),
  128.             $context->getCurrencyId(),
  129.             [ $customer->getLanguageId() ]
  130.         );
  131.         if (
  132.             $attribute['activated']
  133.             && (
  134.                 $this->isFirstActivation
  135.                 || $isMailSendingOnChangeActivation
  136.             )
  137.         ) {
  138.             $this->eventDispatcher->dispatch(new CustomerActivateEvent($customerContext$customer));
  139.         } elseif ($attribute['activated'] === false && $isMailSendingOnChangeActivation) {
  140.             $this->eventDispatcher->dispatch(new CustomerDeactivateEvent($customerContext$customer));
  141.         }
  142.     }
  143.     /**
  144.      * @param RequestEvent $event
  145.      */
  146.     public function onKernelRequest(RequestEvent $event): void
  147.     {
  148.         $attribute $event->getRequest()->get('netiAccessManagerCustomerAttribute');
  149.         if (
  150.             !(
  151.                 \is_array($attribute)
  152.                 && isset($attribute['activated'])
  153.                 && $this->pluginConfig->isActive()
  154.             )
  155.         ) {
  156.             return;
  157.         }
  158.         /** @var CustomerEntity $customer */
  159.         $customer $this->customerRepository->search(new Criteria([$attribute['id']]), Context::createDefaultContext())->first();
  160.         if (!
  161.             (
  162.                 $customer instanceof CustomerEntity
  163.                 && $customer->hasExtension('netiAccessManagerCustomerAttribute')
  164.             )
  165.         ) {
  166.             return;
  167.         }
  168.         /** @var CustomerAttributeEntity $extension */
  169.         $extension $customer->getExtension('netiAccessManagerCustomerAttribute');
  170.         $this->isFirstActivation = (CustomerAttributeEntity::ACTIVATED_BY_SYSTEM === $extension->getActivatedBy());
  171.     }
  172. }