src/EventListener/CartListener.php line 56

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Customer\Customer;
  4. use App\Entity\Vignette\Availability;
  5. use App\Entity\Vignette\Vignette;
  6. use App\Manager\Cart\CartManager;
  7. use App\Model\VignetteAvailabilityModel;
  8. use App\Service\Customer\VehicleService;
  9. use App\Service\CustomerService;
  10. use App\Service\ShoppingCartService;
  11. use App\Service\SystemService;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  16. use App\Entity\Vignette\VignetteApi;
  17. use App\Events\Cart\CartEvent;
  18. use App\Manager\Vignette\VignetteApiManager;
  19. use App\Service\VignetteOrderService;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\RequestStack;
  22. use Symfony\Component\Messenger\TraceableMessageBus;
  23. use Symfony\Component\Translation\IdentityTranslator;
  24. class CartListener
  25. {
  26.     /** @var \Doctrine\ORM\EntityManager|object|null  */
  27.     private $entityManager;
  28.     /** @var Request|null */
  29.     private $request;
  30.     /** @var object|IdentityTranslator|null */
  31.     private $translator;
  32.     /** @var object|TraceableMessageBus|null */
  33.     private $traceableMessageBus;
  34.     /** @var ContainerInterface */
  35.     private $container;
  36.     public function __construct(RequestStack $requestStackContainerInterface $container)
  37.     {
  38.         $this->request $requestStack->getCurrentRequest();
  39.         $this->entityManager $container->get('doctrine.orm.default_entity_manager');
  40.         $this->translator $container->get('translator');
  41.         $this->traceableMessageBus $container->get('messenger.default_bus');
  42.         $this->container $container;
  43.     }
  44.     public function onKernelRequest(GetResponseEvent $event)
  45.     {
  46.         ShoppingCartService::checkShoppingCart($event->getRequest()->getSession(), $this->entityManager$this->traceableMessageBus);
  47.     }
  48.     /**
  49.      * @param CartEvent $cartEvent
  50.      * @throws \Exception
  51.      */
  52.     public function beforeCartSubmitInitRoVignettesToWs(CartEvent $cartEvent)
  53.     {
  54.         if ($roVignettes $cartEvent->getRoVignettes()) {
  55.             $isCreditCard $cartEvent->isPaymentMethodCreditCard();
  56.             foreach ($roVignettes as $roVignette) {
  57.                 $roVignette json_decode(json_encode($roVignette));
  58.                 $availabilityWsId VignetteAvailabilityModel::getAvailabilityWsIdByUrl($roVignette->availability);
  59.                 $availability $this->entityManager->getRepository(Availability::class)->findAvailabilityByWsId($availabilityWsId);
  60.                 if (VignetteAvailabilityModel::isAvailabilityEligibleForCnairAlert($availability) && $roVignette->email_allowed == 1) {
  61.                     $roVignette->email $cartEvent->getCustomerEmail();
  62.                 }
  63.                 if (!isset($roVignette->isInitialized) || !$roVignette->isInitialized) {
  64.                     $this->initRoVignette($roVignette$isCreditCard$cartEvent->getPartnerVignetteAPIKey());
  65.                 }
  66.             }
  67.             $session $this->request->getSession();
  68.             if (!$session->getFlashBag()->has('vignette-init-error')) {
  69.                 VignetteOrderService::checkIfVignetteGlobalOrderIsValidForProcessingOrder($session$this->translator);
  70.             }
  71.         }
  72.     }
  73.     /**
  74.      * @param $roVignette
  75.      * @param $isCreditCard
  76.      * @throws \Exception
  77.      */
  78.     private function initRoVignette($roVignette$isCreditCard$partnerVignetteAPIKey)
  79.     {
  80.         $apiServiceRepository $this->entityManager->getRepository(VignetteApi::class);
  81.         if ($apiServiceRepository->verifyVignette($roVignette$isCreditCard$partnerVignetteAPIKey)) {
  82.             $apiServiceResponse json_decode($apiServiceRepository->getLastResponse());
  83.             if (isset($roVignette->vignette_alert) && $roVignette->vignette_alert == ) {
  84.                 $apiServiceResponse->vignette_alert 1;
  85.             } else {
  86.                 $apiServiceResponse->vignette_alert 0;
  87.             }
  88.             if (isset($roVignette->email_allowed) && $roVignette->email_allowed == ) {
  89.                 $apiServiceResponse->email_allowed 1;
  90.             } else {
  91.                 $apiServiceResponse->email_allowed 0;
  92.             }
  93.             $vignetteOrderService = new VignetteOrderService();
  94.             if (isset($roVignette->chassis_number) && $roVignette->chassis_number) {
  95.                 if (self::verifyIfVinHasChanged($roVignette->chassis_number$apiServiceResponse)) {
  96.                     $message SystemService::retrieveMessage('vin_changed'$this->translator);
  97.                     $message str_replace('%RegistrationPlate%'$roVignette->registration_number$message);
  98.                     $message str_replace('%InitialVIN%'$roVignette->chassis_number$message);
  99.                     $message str_replace('%NewVIN%'$apiServiceResponse->chassis_number$message);
  100.                     $this->request->getSession()->getFlashBag()->set('vignette-vin-changed'$message);
  101.                     $this->updateRoadAssistanceVinForRegistrationPlateIfAvailable($roVignette->registration_number$apiServiceResponse->chassis_number);
  102.                 }
  103.             }
  104.             $vignetteOrderService->storeVignetteInFinalGlobalOrderSession($this->request$apiServiceResponse);
  105.         } else {
  106.             $vignetteApiError VignetteApiManager::retrieveApiErrorResponse($roVignette$this->entityManager$this->translator);
  107.             $this->manageSpecialErrorUpdates($roVignette);
  108.             $this->request->getSession()->getFlashBag()->set('vignette-init-error'implode(','$vignetteApiError));
  109.         }
  110.     }
  111.     /**
  112.      * @param $registrationPlate
  113.      * @param $newVin
  114.      * @return void
  115.      */
  116.     public function updateRoadAssistanceVinForRegistrationPlateIfAvailable($registrationPlate$newVin)
  117.     {
  118.         $session $this->request->getSession();
  119.         if ($allRoadAssistance CartManager::getCartData($session'allRoadAssistance')) {
  120.             foreach ($allRoadAssistance as &$roadAssistance) {
  121.                 if ($registrationPlate == $roadAssistance['registrationPlate']
  122.                     && $roadAssistance['vin'] != $newVin) {
  123.                     $roadAssistance['vin'] = $newVin;
  124.                     break;
  125.                 }
  126.             }
  127.             CartManager::setToCart($session'allRoadAssistance'$allRoadAssistance);
  128.         }
  129.     }
  130.     /**
  131.      * @param $initialVin
  132.      * @param $apiServiceResponse
  133.      * @return bool
  134.      */
  135.     private static function verifyIfVinHasChanged($initialVin$apiServiceResponse): bool
  136.     {
  137.         $hasChanged false;
  138.         if ($apiServiceResponse && isset($apiServiceResponse->chassis_number)
  139.             && $apiServiceResponse->chassis_number) {
  140.             if ($initialVin != $apiServiceResponse->chassis_number) {
  141.                 $hasChanged true;
  142.             }
  143.         }
  144.         return $hasChanged;
  145.     }
  146.     /**
  147.      * @param $roVignette
  148.      * @throws \Exception
  149.      */
  150.     private function manageSpecialErrorUpdates($roVignette)
  151.     {
  152.         $apiServiceRepository $this->entityManager->getRepository(VignetteApi::class);
  153.         $apiStatusCode $apiServiceRepository->getLastResponseCode();
  154.         $specialErrors json_decode($apiServiceRepository->getLastResponse());
  155.         if ($apiStatusCode && $apiStatusCode == Response::HTTP_CONFLICT) {
  156.             if ($this->container->has('security.token_storage')) {
  157.                 $customer CustomerService::retrieveCustomerBySecurityToken($this->container->get('security.token_storage'));
  158.                 if ($customer && $customer instanceof Customer) {
  159.                     $params = array();
  160.                     $params['registrationPlate'] = $roVignette->registration_number;
  161.                     if (isset($roVignette->chassis_number) && $roVignette->chassis_number) {
  162.                         $params['vin'] = $roVignette->chassis_number;
  163.                     }
  164.                     $params['validUntil'] = isset($specialErrors->valid_until) ?  new \DateTime($specialErrors->valid_until) : null;
  165.                     VehicleService::updateVehicleRoVignetteExpiresAtIfAvailable($params$customer$this->entityManager);
  166.                 }
  167.             }
  168.         }
  169.     }
  170. }