custom/static-plugins/SamsonAmpc/src/Subscriber/UserGroupSubscriber.php line 46

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) 2024
  11.  *
  12.  ***/
  13. use Samson\Defaults;
  14. use Samson\Entities\AssetManagement\Corporation\CorporationEntity;
  15. use Samson\Event\UserGroupAddedEvent;
  16. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class UserGroupSubscriber implements EventSubscriberInterface
  21. {
  22.     private EntityRepositoryInterface $customerUserGroupRepository;
  23.     private EntityRepositoryInterface $userGroupRepository;
  24.     public function __construct(
  25.         EntityRepositoryInterface $customerUserGroupRepository,
  26.         EntityRepositoryInterface $userGroupRepository
  27.     )
  28.     {
  29.         $this->customerUserGroupRepository $customerUserGroupRepository;
  30.         $this->userGroupRepository $userGroupRepository;
  31.     }
  32.     /**
  33.      * @inheritDoc
  34.      */
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [UserGroupAddedEvent::class => 'removeUserGroupNewlyRegistered'];
  38.     }
  39.     public function removeUserGroupNewlyRegistered(UserGroupAddedEvent $event)
  40.     {
  41.         $salesChannelContext $event->getContext();
  42.         $customers $event->getCustomers();
  43.         /** @var CorporationEntity $corporation */
  44.         $corporation $salesChannelContext->getCustomer()->getExtension("corporations")->first();
  45.         $criteria = new Criteria();
  46.         $criteria->addFilter(new EqualsFilter("corporationId"$corporation->getId()));
  47.         $criteria->addFilter(new EqualsFilter("name"Defaults::NEWLY_REGISTERED));
  48.         $newlyRegisteredId $this->userGroupRepository->searchIds($criteria$salesChannelContext->getContext())->firstId();
  49.         $filters = [];
  50.         foreach ($customers as $customer) {
  51.             $filters[] = ["customerId" => $customer->getId(), "userGroupId" => $newlyRegisteredId];
  52.         }
  53.         $this->customerUserGroupRepository->delete($filters$salesChannelContext->getContext());
  54.     }
  55. }