<?php
namespace App\EventSubscriber;
use App\Domain\Exception\Client\ClientNotFoundException;
use App\Domain\Middleware\AccountRestrictInterface;
use App\Services\Clients\ClientSystemState\ClientSystemStateService;
use App\Services\Auth\TokenClientUserService;
use App\Entity\Client;
use App\Repository\ClientRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
class ClientAccountCreateSubscriber implements EventSubscriberInterface
{
/**
* @var TokenClientUserService
*/
private $tokenService;
/**
* @var ClientSystemStateService
*/
private $systemStateService;
/**
* @var ClientRepository
*/
private $clientRepository;
public function __construct(
TokenClientUserService $tokenService,
ClientSystemStateService $systemStateService,
ClientRepository $clientRepository
) {
$this->tokenService = $tokenService;
$this->systemStateService = $systemStateService;
$this->clientRepository = $clientRepository;
}
public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();
/*
* $controller passed can be either a class or a Closure.
* This is not usual in Symfony but it may happen.
* If it is a class, it comes in array format
*/
if (!is_array($controller)) {
return;
}
$route = $event->getRequest()->attributes->get('_route');
if ($controller[0] instanceof AccountRestrictInterface && $route == 'clientCreateAccount') {
/** @var Client $currentClient */
$currentClient = $this->tokenService->getCurrentClient();
$this->systemStateService->allowUseSystem($currentClient);
$owner = $event->getRequest()->get('owner');
if ($owner && isset($owner['client_id'])) {
/** @var Client $ownerClient */
$ownerClient = $this->clientRepository->find($owner['client_id']);
if (!$ownerClient) {
throw new ClientNotFoundException();
}
$this->systemStateService->allowUseSystem($ownerClient);
}
}
}
public static function getSubscribedEvents()
{
return [
'kernel.controller' => 'onKernelController',
];
}
}