<?php declare(strict_types=1);
namespace HemoLauter\Subscriber;
use HemoLauter\Util\Constant;
use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupEntity;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Event\DataMappingEvent;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RegistrationSubscriber implements EventSubscriberInterface
{
private EntityRepository $repository;
private SystemConfigService $systemConfigService;
public function __construct(EntityRepository $repository, SystemConfigService $systemConfigService)
{
$this->repository = $repository;
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array
{
return [
AccountLoginPageLoadedEvent::class => 'onPageLoaded',
AccountOverviewPageLoadedEvent::class => 'onPageOverviewLoaded',
CheckoutRegisterPageLoadedEvent::class => 'onPageLoaded',
CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'onCustomerRegister',
];
}
public function onCustomerRegister(DataMappingEvent $event): void
{
$data = $event->getInput();
$group = $this->systemConfigService->get('HemoLauterRegistry.config.customerGroup');
$id = $data->get('groupId');
if ($id && in_array($id, $group)) {
$output = $event->getOutput();
$output['groupId'] = $id;
$event->setOutput($output);
}
}
public function onPageLoaded($event): void
{
$this->getCustomerGroup($event);
}
public function onPageOverviewLoaded($event): void
{
$this->getCustomerGroup($event);
}
/**
* @param $event
* @return void
*/
public function getCustomerGroup($event): void
{
$page = $event->getContext();
$criteria = new Criteria();
$customerGroups = $this->repository->search($criteria, $event->getContext());
$group = $this->systemConfigService->get('HemoLauterRegistry.config.customerGroup');
$page->assign([Constant::CUSTOMER_GROUPS => $customerGroups]);
$page->assign([Constant::FIELD_CONFIG => $group]);
}
}