<?php
declare(strict_types=1);
namespace App\Form\EventListener\Customer;
use App\Client\Qoodos\QoodosClient;
use App\Entity\Security\Customer;
use App\Entity\Security\ShopManager;
use App\Service\Security\CustomerService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class ValidateCustomerFieldsSubscriber implements EventSubscriberInterface
{
public function __construct(
private TokenStorageInterface $tokenStorage,
private QoodosClient $qoodosClient,
private CustomerService $customerService,
) {
}
public static function getSubscribedEvents(): array
{
return [FormEvents::POST_SUBMIT => 'postSubmit'];
}
public function postSubmit(FormEvent $event): void
{
/** @var Customer */
$customer = $event->getData();
/** @var ShopManager */
$currentUser = $this->tokenStorage->getToken()->getUser();
if (!$customer instanceof Customer || !$currentUser instanceof ShopManager) {
return;
}
$shop = $currentUser->getShop();
$franchise = $shop->getFranchise();
$subscription = $this->customerService->addfranchiseSubscriptionTocustomer($customer, $franchise, $shop);
if (true === $franchise->hasQoodos() && null !== $subscription && (null !== $subscription->getQoodosId() || true === $customer->getQoodosSubscription())) {
try {
$this->qoodosClient->init($franchise);
$this->qoodosClient->registerClient($customer);
} catch (\Exception $e) {
}
}
}
}