<?php
declare(strict_types=1);
namespace App\Form\EventListener\User;
use App\Entity\Security\Customer;
use App\Entity\Security\ShopManager;
use App\Form\FormHelper;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class AddQoodosSubscriptionFieldSubscriber extends FormHelper implements EventSubscriberInterface
{
public function __construct(
private TokenStorageInterface $tokenStorage,
) {
}
public static function getSubscribedEvents(): array
{
return [FormEvents::PRE_SET_DATA => 'preSetData'];
}
public function preSetData(FormEvent $event): void
{
/** @var Customer */
$customer = $event->getData();
/** @var ShopManager */
$currentUser = $this->tokenStorage->getToken()->getUser();
if (
true === $customer instanceof Customer
&& null === $customer->getId()
&& true === $currentUser instanceof ShopManager
&& null !== $currentUser->getShop()
) {
$event->getForm()->add('qoodosSubscription', CheckboxType::class, $this->getCheckParameters('app.global.user.list.qoodos.subscription', self::PLACEHOLDER_ENABLE_ASK));
}
}
}