src/EventListener/CartListener.php line 57

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