<?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) 2020
*
***/
use Samson\CustomFieldSet\Constants\CustomerCustomFieldConstants;
use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class AccountSubscriber
* @package Samson\Subscriber
* @author Artur Seitz <artur.seitz@dkd.de>
*/
class AccountSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $corporateFormsRepository;
private EntityRepositoryInterface $customerRepository;
private SystemConfigService $configService;
public function __construct(
EntityRepositoryInterface $corporateFormsRepository,
EntityRepositoryInterface $customerRepository,
SystemConfigService $configService
)
{
$this->corporateFormsRepository = $corporateFormsRepository;
$this->configService = $configService;
$this->customerRepository = $customerRepository;
}
public static function getSubscribedEvents(): array
{
return [
AccountLoginPageLoadedEvent::class => 'onAccountLoginPageLoaded',
CustomerRegisterEvent::class => 'onCustomerRegister'
];
}
public function onAccountLoginPageLoaded(AccountLoginPageLoadedEvent $event): void
{
$criteria = (new Criteria())->addSorting(new FieldSorting('displayName'));
$corporateForms = $this->corporateFormsRepository->search($criteria, $event->getContext())->getEntities();
$event->getPage()->addExtension('corporateForms', $corporateForms);
}
public function onCustomerRegister(CustomerRegisterEvent $event)
{
$salesChannelId = $event->getSalesChannelId();
$adminActivationNeeded = $this->configService->get('SamsonConfiguration.settings.userActivationByAdmin', $salesChannelId);
if ($adminActivationNeeded === null || $adminActivationNeeded === false) {
return;
}
$this->customerRepository->update([[
"id" => $event->getCustomer()->getId(),
"customFields" => [CustomerCustomFieldConstants::CUSTOM_FIELD_ADMIN_ACTIVATION_NEEDED => true]
]], $event->getContext());
}
}