<?php
declare(strict_types=1);
namespace App\Component\Sanctions;
use App\Controller\LeadRegistrantController;
use App\Controller\MySief\SanctionsController;
use App\Controller\MySief\Substances\Member\SubstancesController as MemberSubstancesController;
use App\Controller\MySief\Substances\NonMember\SubstancesController as NonMemberSubstancesController;
use App\Entity\LeadRegistrant;
use App\Entity\LeadRegistrant\DossierUpdate;
use App\Entity\LegalEntity;
use App\Entity\User;
use App\Service\UserLookup;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
class ForceTasksListener
{
private SanctionsInterface $sanctions;
private SanctionsController $controller;
private UserLookup $userLookup;
private EntityManagerInterface $em;
public function __construct(
SanctionsInterface $sanctions,
SanctionsController $controller,
UserLookup $userLookup,
EntityManagerInterface $em
) {
$this->sanctions = $sanctions;
$this->controller = $controller;
$this->userLookup = $userLookup;
$this->em = $em;
}
public function onKernelController(ControllerEvent $event): void
{
$user = $this->userLookup->lookup();
if (
!$user instanceof User
|| !$this->sanctions->isActive()
|| $user->isStaff()
|| 0 === $user->getValidApprovedEntityCount()
) {
return;
}
$controller = $event->getController();
if (is_array($controller)) {
$controller = $controller[0];
}
if ($controller instanceof SanctionsController) {
return;
}
$entityId = null;
$attributes = $event->getRequest()->attributes;
if ($controller instanceof MemberSubstancesController) {
$entityId = $attributes->get('entity');
} elseif ($controller instanceof NonMemberSubstancesController) {
$entityId = $attributes->get('entity');
} elseif ($controller instanceof LeadRegistrantController) {
$updateId = $attributes->get('update');
$lrId = $attributes->get('leadRegistrant');
if (null !== $updateId) {
$update = $this->em->find(DossierUpdate::class, $updateId);
$entityId = $update->getLeadRegistrant()->getLegalEntity()->getId();
} elseif (null !== $lrId) {
$lr = $this->em->find(LeadRegistrant::class, $lrId);
$entityId = $lr->getLegalEntity()->getId();
}
}
if (null !== $entityId) {
$entity = $this->em->find(LegalEntity::class, $entityId);
if (0 === $entity->getOrderCount()) {
return;
}
if (!$this->sanctions->isAccessDenied($user, $entity)) {
return;
}
} else {
if (!$this->sanctions->isAccessDenied($user)) {
return;
}
}
$event->getRequest()->attributes->set('sanctions_enforcement', true);
$event->setController([$this->controller, 'indexAction']);
$event->stopPropagation();
}
}