<?php
namespace App\EventSubscriber;
use App\Domain\Middleware\ClientCompanyCreateInterface;
use App\Services\Clients\ClientSystemState\ClientSystemStateService;
use App\Services\Auth\TokenClientUserService;
use App\Entity\Client;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
class ClientCompanyCreateSubscriber implements EventSubscriberInterface
{
/**
* @var TokenClientUserService
*/
private $tokenService;
/**
* @var ClientSystemStateService
*/
private $systemStateService;
public function __construct(
TokenClientUserService $tokenService,
ClientSystemStateService $systemStateService
) {
$this->tokenService = $tokenService;
$this->systemStateService = $systemStateService;
}
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 ClientCompanyCreateInterface && $route == 'client_create_company') {
/** @var Client $currentClient */
$currentClient = $this->tokenService->getCurrentClient();
$this->systemStateService->allowUseSystem($currentClient);
}
}
public static function getSubscribedEvents()
{
return [
'kernel.controller' => 'onKernelController',
];
}
}