src/EventListener/VignetteListener.php line 68

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\Category;
  6. use App\Entity\Vignette\Price;
  7. use App\Entity\Vignette\VignetteApi;
  8. use App\Events\VignetteEvent;
  9. use App\Manager\Partner\PartnerManager;
  10. use App\Manager\Vignette\CategoryManager;
  11. use App\Model\Vignette\ValidityModel;
  12. use App\Service\Customer\VehicleService;
  13. use App\Service\CustomerService;
  14. use App\Service\SystemService;
  15. use App\Service\VignetteApiService;
  16. use App\Service\VignetteOrderService;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use Symfony\Component\DependencyInjection\ContainerInterface;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. class VignetteListener
  21. {
  22.     protected $listener;
  23.     protected $entityManager;
  24.     protected $container;
  25.     protected $event;
  26.     protected $request;
  27.     public function __construct(VignetteEvent $listenerEntityManagerInterface $entityManager
  28.         ContainerInterface $containerRequestStack $requestStack)
  29.     {
  30.         $this->listener $listener;
  31.         $this->entityManager $entityManager;
  32.         $this->container $container;
  33.         $this->request $requestStack->getCurrentRequest();
  34.     }
  35.     /**
  36.      * @return EntityManagerInterface
  37.      */
  38.     public function getEntityManager(): EntityManagerInterface
  39.     {
  40.         return $this->entityManager;
  41.     }
  42.     /**
  43.      * @return ContainerInterface
  44.      */
  45.     public function getContainer(): ContainerInterface
  46.     {
  47.         return $this->container;
  48.     }
  49.     /**
  50.      * @return VignetteEvent
  51.      */
  52.     public function getListener(): VignetteEvent
  53.     {
  54.         return $this->listener;
  55.     }
  56.     /**
  57.      * @throws \Exception
  58.      */
  59.     public function checkVignette()
  60.     {
  61.         if ($this->request->request->has('specialErrors')) {
  62.             $this->request->request->remove('specialErrors');
  63.             $this->request->request->remove('apiStatusCode');
  64.         }
  65.         if ($this->request->request->has('vignetteSubmitForm')) {
  66.             $this->request->request->remove('vignetteSubmitForm');
  67.         }
  68.         $apiService = new VignetteApiService($this->getEntityManager());
  69.         $params $this->request->request->all();
  70.         $data $apiService->constructInitDataArray($params);
  71.         $apiServiceRepository $this->getEntityManager()->getRepository(VignetteApi::class);
  72.         if (!empty($data)) {
  73.             $apiResponse $apiServiceRepository->checkRoVignette($data);
  74.             $ignoreAlreadyActive $this->request->get('ignoreAlreadyActive') === 'true';
  75.             $fullIntersection false;
  76.             if ($apiResponse && isset($apiResponse['intervals']) && $apiResponse['intervals']) {
  77.                 $lastKey array_key_last($apiResponse['intervals']);
  78.                 if ($fullIntersection $this->checkIfVignetteHasFullIntersection($apiResponse['intervals'][$lastKey], $data)) {
  79.                     if ($apiResponse['isCategoryValid']) {
  80.                         $ignoreAlreadyActive false;
  81.                     }
  82.                 }
  83.             }
  84.             $ignoreWrongCategory $this->request->get('ignoreWrongCategory') === 'true';
  85.             if ($ignoreWrongCategory) {
  86.                 $ignoreAlreadyActive true;
  87.             }
  88.             $isAvailabilityValid = !$apiResponse['alreadyActive'] || $ignoreAlreadyActive;
  89.             $isCategoryValid $apiResponse['isCategoryValid'] || $ignoreWrongCategory;
  90.             if ($apiResponse['isCategoryValid'] && $fullIntersection) {
  91.                 $isAvailabilityValid false;
  92.             }
  93.             if ($apiResponse && $isAvailabilityValid && $isCategoryValid) {
  94.                 if (isset($data['vignette_alert']) && $data['vignette_alert'] == ) {
  95.                     $data['vignette_alert'] = 1;
  96.                 } else {
  97.                     $data['vignette_alert'] = 0;
  98.                 }
  99.                 if (isset($data['email_allowed']) && $data['email_allowed'] == 1){
  100.                     $data['email_allowed'] = 1;
  101.                 } else {
  102.                     $data['email_allowed'] = 0;
  103.                 }
  104.                 if ($this->request->get('partnerQueryParams')) {
  105.                     $session $this->request->getSession();
  106.                     $session->set('partnerQueryParams',$this->request->get('partnerQueryParams'));
  107.                 }
  108.                 $data['publicToken'] = $params['publicToken'] ?? null;
  109.                 $vignetteOrderService = new VignetteOrderService();
  110.                 $data $this->constructVignetteDataToStore($data);
  111.                 $vignetteOrderService->storeVignetteInGlobalOrderSession($this->request$data);
  112.                 $this->request->request->set('vignetteSubmitForm'1);
  113.             } else {
  114.                 $errors '';
  115.                 if (!$isAvailabilityValid) {
  116.                    $errors $this->retrieveAlreadyActiveIntervalsMessage($apiResponse$fullIntersection);
  117.                    if ($errors) {
  118.                        if ($this->container->has('security.token_storage')) {
  119.                            $customer CustomerService::retrieveCustomerBySecurityToken($this->container->get('security.token_storage'));
  120.                            if ($customer && $customer instanceof Customer
  121.                                && isset($params['vin']) && $params['vin']) {
  122.                                $validUntil null;
  123.                                foreach ($apiResponse['intervals'] as $key => $interval) {
  124.                                    $validUntil = new \DateTime($interval->valid_until);
  125.                                }
  126.                                $params['validUntil'] = $validUntil;
  127.                                VehicleService::updateVehicleRoVignetteExpiresAtIfAvailable($params$customer$this->entityManager);
  128.                            }
  129.                        }
  130.                    }
  131.                 }
  132.                 if (!$isCategoryValid) {
  133.                     if ($errors) {
  134.                         $errors $errors ' ' $this->retrieveWrongCategoryMessage($apiResponse['validCategoryId']);
  135.                     } else {
  136.                         $errors $this->retrieveWrongCategoryMessage($apiResponse['validCategoryId']);
  137.                     }
  138.                 }
  139.                 $this->request->request->set('specialErrors'$errors);
  140.             }
  141.         }
  142.     }
  143.     /**
  144.      * @param $validCategoryId
  145.      * @return string|string[]
  146.      */
  147.     public function retrieveWrongCategoryMessage($validCategoryId)
  148.     {
  149.         $translator $this->getContainer()->get('translator');
  150.         $message SystemService::retrieveMessage('valid_category_from_ws'$translator);
  151.         $categoryName SystemService::retrieveMessage('category_rovignette_' CategoryManager::retrieveCategoryShortCodeById($validCategoryId) . '_name',
  152.             $translator);
  153.         $message str_replace('%CategoryName%'$categoryName$message);
  154.         return $message;
  155.     }
  156.     /**
  157.      * @param $apiResponse
  158.      * @param $fullIntersection
  159.      * @return string|null
  160.      * @throws \Exception
  161.      */
  162.     private function retrieveAlreadyActiveIntervalsMessage($apiResponse$fullIntersection): ?string
  163.     {
  164.         $intervals $apiResponse['intervals'];
  165.         $translator $this->getContainer()->get('translator');
  166.         $message SystemService::retrieveMessage('already_active_intervals'$translator);
  167.         $lastKey array_key_last($intervals);
  168.         foreach ($intervals as $key => $interval) {
  169.             $validFrom = new \DateTime($interval->valid_from);
  170.             $validUntil = new \DateTime($interval->valid_until);
  171.             $message .= $validFrom->format('Y-m-d H:i:s') . ' - ' $validUntil->format('Y-m-d H:i:s');
  172.             if ($key != $lastKey) {
  173.                 $message .= ', ';
  174.             }
  175.         }
  176.         if (isset($apiResponse['vin']) && $apiResponse['vin']) {
  177.             $continueMessage SystemService::retrieveMessage('already_active_intervals_vin'$translator);
  178.             $continueMessage $continueMessage $apiResponse['vin'];
  179.             $message $message $continueMessage;
  180.         }
  181.         if ($fullIntersection) {
  182.             $continueMessage SystemService::retrieveMessage('already_active_intervals_continue'$translator);
  183.             $message $message $continueMessage;
  184.         }
  185.         return $message;
  186.     }
  187.     /**
  188.      * @param $data
  189.      * @return \stdClass
  190.      */
  191.     private function constructVignetteDataToStore($data): \stdClass
  192.     {
  193.         $category $this->entityManager->getRepository(Category::class)->findCategoryByUrl($data['category']);
  194.         $availability $this->entityManager->getRepository(Availability::class)->findAvailabilityByUrl($data['availability']);
  195.         $price $this->entityManager->getRepository(Price::class)->getPriceForCategoryAndAvailability($category$availability);
  196.         $data['price'] = $price->getCustomerPrice();
  197.         $data['guid'] = SystemService::generateRandomString(32);
  198.         if (isset($data['valid_from']) && isset($data['availability'])) {
  199.             $validUntil ValidityModel::retrieveValidUntilByValidFromAndAvailability(new \DateTime($data['valid_from']),
  200.                 $this->entityManager->getRepository(Availability::class)->findAvailabilityByUrl($data['availability']));
  201.             $data['valid_until'] = $validUntil $validUntil->format(\DateTime::ATOM) : null;
  202.         }
  203.         //convert to stObject
  204.         $data json_decode(json_encode($data));
  205.         return $data;
  206.     }
  207.     /**
  208.      * @param $activeIntervals
  209.      * @param $vignetteParams
  210.      * @return bool
  211.      * @throws \Exception
  212.      */
  213.     private function checkIfVignetteHasFullIntersection($activeIntervals$vignetteParams): bool
  214.     {
  215.         $isFullIntersection false;
  216.         $validFrom = new \DateTime($vignetteParams['valid_from']);
  217.         $validUntil ValidityModel::retrieveValidUntilByValidFromAndAvailability($validFrom,
  218.             $this->entityManager->getRepository(Availability::class)->findAvailabilityByUrl($vignetteParams['availability']));
  219.         $validFrom = new \DateTime($vignetteParams['valid_from']);
  220.         if ($validUntil) {
  221.             $activeStartDate = new \DateTime($activeIntervals->valid_from);
  222.             $activeEndDate = new \DateTime($activeIntervals->valid_until);
  223.             if ($activeStartDate->format('Y-m-d') == $validFrom->format('Y-m-d')
  224.                 && $activeEndDate->format('Y-m-d') == $validUntil->format('Y-m-d')) {
  225.                 $isFullIntersection true;
  226.             }
  227.         }
  228.         return $isFullIntersection;
  229.     }
  230. }