src/Form/EventListener/User/AddQoodosNotificationPhoneFieldSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\EventListener\User;
  4. use App\Entity\Security\Customer;
  5. use App\Entity\Security\ShopManager;
  6. use App\Form\FormHelper;
  7. use App\Service\Security\CustomerService;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  10. use Symfony\Component\Form\FormEvent;
  11. use Symfony\Component\Form\FormEvents;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. class AddQoodosNotificationPhoneFieldSubscriber extends FormHelper implements EventSubscriberInterface
  14. {
  15.     public function __construct(
  16.         private TokenStorageInterface $tokenStorage,
  17.         private CustomerService $customerService,
  18.     ) {
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [FormEvents::PRE_SET_DATA => 'preSetData'];
  23.     }
  24.     public function preSetData(FormEvent $event): void
  25.     {
  26.         /** @var Customer */
  27.         $customer $event->getData();
  28.         /** @var ShopManager */
  29.         $currentUser $this->tokenStorage->getToken()->getUser();
  30.         if (false === $customer instanceof Customer || false === $currentUser instanceof ShopManager) {
  31.             return;
  32.         }
  33.         $shop $currentUser->getShop();
  34.         if (null === $shop) {
  35.             return;
  36.         }
  37.         if (null !== $customer->getId()) {
  38.             $subscription $this->customerService->getSubscription($customer$shop->getFranchise());
  39.             if (null === $subscription || null === $subscription->getQoodosId()) {
  40.                 return;
  41.             }
  42.             $customer->setQoodosNotificationPhone($subscription->getSmsNotification());
  43.             $event->setData($customer);
  44.         }
  45.         $event->getForm()->add('qoodosNotificationPhone'CheckboxType::class, $this->getCheckParameters('app.global.user.list.qoodos.notification_sms'self::PLACEHOLDER_ENABLE_ASK));
  46.     }
  47. }