<?php
declare(strict_types=1);
namespace App\Form\EventListener\Franchise;
use App\Entity\Franchise\Franchise;
use App\Entity\Security\RoleInterface;
use App\Form\FormHelper;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Security\Core\Security;
class AddStatusFranchiseFieldSubscriber extends FormHelper implements EventSubscriberInterface
{
public function __construct(
private Security $security,
) {
}
public static function getSubscribedEvents(): array
{
return [FormEvents::PRE_SET_DATA => 'preSetData'];
}
public function preSetData(FormEvent $event): void
{
/** @var Franchise $franchise */
$franchise = $event->getData();
if (
0 === \count(array_intersect([RoleInterface::ROLE_ADMIN, RoleInterface::ROLE_PREVIOUS_ADMIN], $this->security->getToken()->getRoleNames()))
|| (null !== $franchise && null === $franchise->getId())
) {
return;
}
$choices = [];
foreach (Franchise::STATUS as $status) {
$choices['app.manager.pages.franchise.status.' . $status] = $status;
}
$event->getForm()->add('status', ChoiceType::class, $this->getChoiceParameters('app.global.select.status', self::PLACEHOLDER_CHOICE, true, compact('choices')));
}
}