<?php declare(strict_types=1);
namespace Samson\Subscriber;
/***
*
* This file is part of the "SAMSON Shop" project.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2024
*
***/
use Samson\Defaults;
use Samson\Entities\AssetManagement\Corporation\CorporationEntity;
use Samson\Event\UserGroupAddedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UserGroupSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $customerUserGroupRepository;
private EntityRepositoryInterface $userGroupRepository;
public function __construct(
EntityRepositoryInterface $customerUserGroupRepository,
EntityRepositoryInterface $userGroupRepository
)
{
$this->customerUserGroupRepository = $customerUserGroupRepository;
$this->userGroupRepository = $userGroupRepository;
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
return [UserGroupAddedEvent::class => 'removeUserGroupNewlyRegistered'];
}
public function removeUserGroupNewlyRegistered(UserGroupAddedEvent $event)
{
$salesChannelContext = $event->getContext();
$customers = $event->getCustomers();
/** @var CorporationEntity $corporation */
$corporation = $salesChannelContext->getCustomer()->getExtension("corporations")->first();
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter("corporationId", $corporation->getId()));
$criteria->addFilter(new EqualsFilter("name", Defaults::NEWLY_REGISTERED));
$newlyRegisteredId = $this->userGroupRepository->searchIds($criteria, $salesChannelContext->getContext())->firstId();
$filters = [];
foreach ($customers as $customer) {
$filters[] = ["customerId" => $customer->getId(), "userGroupId" => $newlyRegisteredId];
}
$this->customerUserGroupRepository->delete($filters, $salesChannelContext->getContext());
}
}