src/EventListener/VignetteListener.php line 70

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