<?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 App\Service\Security\CustomerService;
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 AddQoodosNotificationPhoneFieldSubscriber extends FormHelper implements EventSubscriberInterface
{
public function __construct(
private TokenStorageInterface $tokenStorage,
private CustomerService $customerService,
) {
}
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 (false === $customer instanceof Customer || false === $currentUser instanceof ShopManager) {
return;
}
$shop = $currentUser->getShop();
if (null === $shop) {
return;
}
if (null !== $customer->getId()) {
$subscription = $this->customerService->getSubscription($customer, $shop->getFranchise());
if (null === $subscription || null === $subscription->getQoodosId()) {
return;
}
$customer->setQoodosNotificationPhone($subscription->getSmsNotification());
$event->setData($customer);
}
$event->getForm()->add('qoodosNotificationPhone', CheckboxType::class, $this->getCheckParameters('app.global.user.list.qoodos.notification_sms', self::PLACEHOLDER_ENABLE_ASK));
}
}