custom/static-plugins/SamsonCustomer/src/Subscriber/AccountSubscriber.php line 57

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Samson\Subscriber;
  3. /***
  4.  *
  5.  * This file is part of the "SAMSON Shop" project.
  6.  *
  7.  * For the full copyright and license information, please read the
  8.  * LICENSE.txt file that was distributed with this source code.
  9.  *
  10.  *  (c) 2020
  11.  *
  12.  ***/
  13. use Samson\CustomFieldSet\Constants\CustomerCustomFieldConstants;
  14. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  18. use Shopware\Core\System\SystemConfig\SystemConfigService;
  19. use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. /**
  22.  * Class AccountSubscriber
  23.  * @package Samson\Subscriber
  24.  * @author Artur Seitz <artur.seitz@dkd.de>
  25.  */
  26. class AccountSubscriber implements EventSubscriberInterface
  27. {
  28.     private EntityRepositoryInterface $corporateFormsRepository;
  29.     private EntityRepositoryInterface $customerRepository;
  30.     private SystemConfigService $configService;
  31.     public function __construct(
  32.         EntityRepositoryInterface $corporateFormsRepository,
  33.         EntityRepositoryInterface $customerRepository,
  34.         SystemConfigService       $configService
  35.     )
  36.     {
  37.         $this->corporateFormsRepository $corporateFormsRepository;
  38.         $this->configService $configService;
  39.         $this->customerRepository $customerRepository;
  40.     }
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44.             AccountLoginPageLoadedEvent::class => 'onAccountLoginPageLoaded',
  45.             CustomerRegisterEvent::class => 'onCustomerRegister'
  46.         ];
  47.     }
  48.     public function onAccountLoginPageLoaded(AccountLoginPageLoadedEvent $event): void
  49.     {
  50.         $criteria = (new Criteria())->addSorting(new FieldSorting('displayName'));
  51.         $corporateForms $this->corporateFormsRepository->search($criteria$event->getContext())->getEntities();
  52.         $event->getPage()->addExtension('corporateForms'$corporateForms);
  53.     }
  54.     public function onCustomerRegister(CustomerRegisterEvent $event)
  55.     {
  56.         $salesChannelId $event->getSalesChannelId();
  57.         $adminActivationNeeded $this->configService->get('SamsonConfiguration.settings.userActivationByAdmin'$salesChannelId);
  58.         if ($adminActivationNeeded === null || $adminActivationNeeded === false) {
  59.             return;
  60.         }
  61.         $this->customerRepository->update([[
  62.             "id" => $event->getCustomer()->getId(),
  63.             "customFields" => [CustomerCustomFieldConstants::CUSTOM_FIELD_ADMIN_ACTIVATION_NEEDED => true]
  64.         ]], $event->getContext());
  65.     }
  66. }