src/Form/EventListener/Customer/ValidateCustomerFieldsSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\EventListener\Customer;
  4. use App\Client\Qoodos\QoodosClient;
  5. use App\Entity\Security\Customer;
  6. use App\Entity\Security\ShopManager;
  7. use App\Service\Security\CustomerService;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Form\FormEvent;
  10. use Symfony\Component\Form\FormEvents;
  11. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  12. class ValidateCustomerFieldsSubscriber implements EventSubscriberInterface
  13. {
  14.     public function __construct(
  15.         private TokenStorageInterface $tokenStorage,
  16.         private QoodosClient $qoodosClient,
  17.         private CustomerService $customerService,
  18.     ) {
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [FormEvents::POST_SUBMIT => 'postSubmit'];
  23.     }
  24.     public function postSubmit(FormEvent $event): void
  25.     {
  26.         /** @var Customer */
  27.         $customer $event->getData();
  28.         /** @var ShopManager */
  29.         $currentUser $this->tokenStorage->getToken()->getUser();
  30.         if (!$customer instanceof Customer || !$currentUser instanceof ShopManager) {
  31.             return;
  32.         }
  33.         $shop $currentUser->getShop();
  34.         $franchise $shop->getFranchise();
  35.         $subscription $this->customerService->addfranchiseSubscriptionTocustomer($customer$franchise$shop);
  36.         if (true === $franchise->hasQoodos() && null !== $subscription && (null !== $subscription->getQoodosId() || true === $customer->getQoodosSubscription())) {
  37.             try {
  38.                 $this->qoodosClient->init($franchise);
  39.                 $this->qoodosClient->registerClient($customer);
  40.             } catch (\Exception $e) {
  41.             }
  42.         }
  43.     }
  44. }