src/PortalBundle/Services/VehicleService.php line 174

Open in your IDE?
  1. <?php
  2. namespace PortalBundle\Services;
  3. use CoreBundle\Entity\Brand;
  4. use CoreBundle\Entity\Dealer;
  5. use CoreBundle\Entity\Featured;
  6. use CoreBundle\Entity\Model;
  7. use CoreBundle\Entity\Vehicles\InStock;
  8. use CoreBundle\Entity\Vehicles\Vehicle;
  9. use CoreBundle\Factory\Vehicle as VehicleFactory;
  10. use CoreBundle\Model\Vehicles\AbstractVehicle;
  11. use CoreBundle\Model\Vehicles\VehicleType;
  12. use CoreBundle\Services\MediaExtensionVidi;
  13. use DcSiteBundle\Model\CreditModel;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use PortalBundle\Model\Catalog;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Component\Routing\RouterInterface;
  18. class VehicleService
  19. {
  20. private EntityManagerInterface       $em;
  21. private RouterInterface             $router;
  22. private MediaExtensionVidi  $mediaExtension;
  23. private VehicleFactory      $vehicleFactory;
  24. private RequestStack        $requestStack;
  25. private CreditModel         $creditModel;
  26.     public function __construct(
  27.         EntityManagerInterface       $em,
  28.         RouterInterface             $router,
  29.         MediaExtensionVidi  $mediaExtension,
  30.         VehicleFactory      $vehicleFactory,
  31.         RequestStack        $requestStack,
  32.         CreditModel         $creditModel
  33.     )
  34.     {
  35.         $this->em $em;
  36.         $this->router $router;
  37.         $this->mediaExtension $mediaExtension;
  38.         $this->vehicleFactory $vehicleFactory;
  39.         $this->requestStack $requestStack;
  40.         $this->creditModel $creditModel;
  41.     }
  42.     public function getVehicles()
  43.     {
  44.     }
  45.     public function getComparison(AbstractVehicle $vehicle)
  46.     {
  47.         $request $this->requestStack->getCurrentRequest();
  48.         $compareCookie $request->cookies->get('compare');
  49.         $vehicleComparison explode(','$compareCookie);
  50.         return in_array($vehicle->getVehicleItemId(),$vehicleComparison)? $vehicle->getVehicleItemId() : null;
  51.     }
  52.     public function getFeatures($user): array
  53.     {
  54.         $featuresIds = [];
  55.         if(!$user){
  56.             return $featuresIds;
  57.         }
  58.         $features $this->em->getRepository(Featured::class)->findBy(['type' => 'vehicle''user' => $user]);
  59.         /** @var Featured $feature */
  60.         foreach ($features as $feature) {
  61.             $favData json_decode($feature->getData());
  62.             if(!isset($favData->vehicleItemId)){
  63.                 continue;
  64.             }
  65.             $featuresIds[$favData->vehicleItemId] = $feature->getId();
  66.         }
  67.         return $featuresIds;
  68.     }
  69.     public function getVehicleInStock(Model $model$userDealer $dealer null,$limit null)
  70.     {
  71.         $inStockVehicles $this->em->getRepository(InStock::class)->getInStockVehicleByModel($model$dealer$limit);
  72.         $featuresIds $this->getFeatures($user);
  73.         $items = [];
  74.         /** @var InStock $item */
  75.         foreach ($inStockVehicles as $item) {
  76.             $vehicle $this->vehicleFactory->createByVehicleItem($item->getVehicleItem());
  77.             if (!$vehicle) {
  78.                 continue;
  79.             }
  80.             $items[] = [
  81.                 'vehicleType' => VehicleType::getTypeDataById($vehicle->getVehicleType()),
  82.                 'creditPayment' => $this->creditModel->getMinPayment($vehicle),
  83.                 'vehicle' => $vehicle,
  84.                 'link' => $this->router->generate('portal_in_stock_one',['dealer' => $vehicle->getDealer()->getUrl(), 'url' => $item->getUrl()]),
  85.                 'featuredId' => $featuresIds[$vehicle->getVehicleItemId()] ?? null,
  86.                 'comparedId' => $this->getComparison($vehicle),
  87.                 'vehiclePrice' => $vehicle->fullPrice(),
  88.                 'vehicleColors' => $this->getVehicleColors($vehicle),
  89.                 'hasNds' => false,
  90.                 'isSelect' => false,
  91.             ];
  92.         }
  93.         return $items;
  94.     }
  95.     public function getVehicleInStockByBodyType($user$characteristicId$price null$limit null)
  96.     {
  97.         $inStockVehicles $this->em->getRepository(InStock::class)->getInStockVehicleByBodyType($characteristicId);
  98.         $featuresIds $this->getFeatures($user);
  99.         $items = [];
  100.         /** @var InStock $item */
  101.         foreach ($inStockVehicles as $item) {
  102.             $vehicle $this->vehicleFactory->createByVehicleItem($item->getVehicleItem());
  103.             if (!$vehicle || $item->calcPrice() == 0) {
  104.                 continue;
  105.             }
  106.             $minPriceRange $price 0.8;
  107.             $maxPriceRange $price 1.2;
  108.             $vehicleItemPrice $item->calcPrice();
  109.             if ($vehicleItemPrice $minPriceRange || $vehicleItemPrice $maxPriceRange){
  110.                 continue;
  111.             }
  112.             $items[] = [
  113.                 'preview' => $item->getPreview(),
  114.                 'vehicleType' => VehicleType::getTypeDataById($vehicle->getVehicleType()),
  115.                 'creditPayment' => $this->creditModel->getMinPayment($vehicle),
  116.                 'vehicle' => $vehicle,
  117.                 'link' => $this->router->generate('portal_in_stock_one',['dealer' => $vehicle->getDealer()->getUrl(), 'url' => $item->getUrl()]),
  118.                 'featuredId' => $featuresIds[$vehicle->getVehicleItemId()] ?? null,
  119.                 'comparedId' => $this->getComparison($vehicle),
  120.                 'vehiclePrice' => $item->calcPrice(),
  121.                 'vehicleColors' => [],
  122.                 'hasNds' => false,
  123.                 'isSelect' => false,
  124.             ];
  125.         }
  126.         return count($items) > $limit array_slice($items0,$limit) : $items;
  127.     }
  128.     public function getVehicleInStockByBrand(Brand $brand$user$vehicleTypeId$limit)
  129.     {
  130.         $inStockVehicles $this->em->getRepository(InStock::class)->getInStockVehicleByBrand($brand$vehicleTypeId$limit);
  131.         $featuresIds $this->getFeatures($user);
  132.         $items = [];
  133.         /** @var InStock $item */
  134.         foreach ($inStockVehicles as $item) {
  135.             $vehicle $this->vehicleFactory->createByVehicleItem($item->getVehicleItem());
  136.             if (!$vehicle) continue;
  137.             $items[] = [
  138.                 'preview' => $item->getPreview(),
  139.                 'vehicleType' => VehicleType::getTypeDataById($vehicle->getVehicleType()),
  140.                 'creditPayment' => $this->creditModel->getMinPayment($vehicle),
  141.                 'vehicle' => $vehicle,
  142.                 'link' => $this->router->generate('portal_in_stock_one',['dealer' => $vehicle->getDealer()->getUrl(), 'url' => $item->getUrl()]),
  143.                 'featuredId' => $featuresIds[$vehicle->getVehicleItemId()] ?? null,
  144.                 'comparedId' => $this->getComparison($vehicle),
  145.                 'vehiclePrice' => $item->calcPrice(),
  146.                 'vehicleColors' => [],
  147.                 'hasNds' => false,
  148.                 'isSelect' => false,
  149.             ];
  150.         }
  151.         return $items;
  152.     }
  153.     public function getVehicleUsed(Model $model$user$limit)
  154.     {
  155.         $usedVehicle $this->em->getRepository(\CoreBundle\Entity\Vehicles\Vehicle::class)->getUsedVehicleByModel($model$limit);
  156.         $featuresIds $this->getFeatures($user);
  157.         $items = [];
  158.         /** @var Vehicle $item */
  159.         foreach ($usedVehicle as $item) {
  160.             $vehicle $this->vehicleFactory->createUsedById($item->getId());
  161.             if (!$vehicle) {
  162.                 continue;
  163.             }
  164.             $items[] = [
  165.                 'preview' => $item->getPreview(),
  166.                 'vehicleType' => VehicleType::getTypeDataById($vehicle->getVehicleType()),
  167.                 'creditPayment' => $this->creditModel->getMinPayment($vehicle),
  168.                 'vehicle' => $vehicle,
  169.                 'link' => $this->router->generate('portal_used_car', ['url' => $vehicle->getUrl()]),
  170.                 'featuredId' => $featuresIds[$vehicle->getVehicleItemId()] ?? null,
  171.                 'comparedId' => $this->getComparison($vehicle),
  172.                 'vehiclePrice' => $vehicle->fullPrice(),
  173.                 'vehicleColors' => $this->getVehicleColors($vehicle),
  174.                 'hasNds' => false,
  175.                 'isSelect' => !in_array($vehicle->getDealer()->getId(), Catalog::NOT_VIDI_SELECT_DEALERS),
  176.             ];
  177.         }
  178.         return $items;
  179.     }
  180.     public function getVehicleUsedByBrand(Brand $brand$user$vehicleTypeId$limit)
  181.     {
  182.         $usedVehicle $this->em->getRepository(\CoreBundle\Entity\Vehicles\Vehicle::class)->getUsedVehicleByBrand($brand$vehicleTypeId$limit);
  183.         $featuresIds $this->getFeatures($user);
  184.         $items = [];
  185.         /** @var Vehicle $item */
  186.         foreach ($usedVehicle as $item) {
  187.             $vehicle $this->vehicleFactory->createUsedById($item->getId());
  188.             if (!$vehicle) {
  189.                 continue;
  190.             }
  191.             $items[] = [
  192.                 'preview' => $item->getPreview(),
  193.                 'vehicleType' => VehicleType::getTypeDataById($vehicle->getVehicleType()),
  194.                 'creditPayment' => $this->creditModel->getMinPayment($vehicle),
  195.                 'vehicle' => $vehicle,
  196.                 'link' => $this->router->generate('portal_used_car', ['url' => $vehicle->getUrl()]),
  197.                 'featuredId' => $featuresIds[$vehicle->getVehicleItemId()] ?? null,
  198.                 'comparedId' => $this->getComparison($vehicle),
  199.                 'vehiclePrice' => $vehicle->fullPrice(),
  200.                 'vehicleColors' => $this->getVehicleColors($vehicle),
  201.                 'hasNds' => false,
  202.                 'isSelect' => !in_array($vehicle->getDealer()->getId(), Catalog::NOT_VIDI_SELECT_DEALERS),
  203.             ];
  204.         }
  205.         return $items;
  206.     }
  207.     public function getVehicleByCharacteristicId($user$characteristicId$isUsed false$limit null$price null$modelId null$limitRangeCount null): array
  208. {
  209.         $vehicleCharacteristic $this->em->getRepository(\CoreBundle\Entity\Vehicles\Vehicle::class)->getVehicleByCharacteristicId($characteristicIdnull$isUsed$limit);
  210.         $featuresIds $this->getFeatures($user);
  211.         $items = [];
  212.         /** @var Vehicle $item */
  213.         foreach ($vehicleCharacteristic as $item) {
  214.             $vehicles = [];
  215.             foreach ($item->getVehicleItems()->filter(
  216.                 fn($vehicleItem) => $vehicleItem->getState()
  217.             ) as $vehicleItem) {
  218.                 $vehicles[] = $this->vehicleFactory->createByVehicleItem($vehicleItem);
  219.             }
  220.             foreach ($vehicles as $vehicle) {
  221.                 if(!$vehicle || $modelId == $vehicle->getModel()->getId() || $vehicle->price() == 0){
  222.                     continue;
  223.                 }
  224.                 if ($price){
  225.                     $minPriceRange $price 0.8;
  226.                     $maxPriceRange $price 1.2;
  227.                     $vehicleItemPrice $vehicle->price();
  228.                     if ($vehicleItemPrice $minPriceRange || $vehicleItemPrice $maxPriceRange){
  229.                         continue;
  230.                     }
  231.                 }
  232.                 if($isUsed){
  233.                     $link $this->router->generate('portal_used_car', ['dealer' => $vehicle->getDealer()->getUrl(), 'url' => $vehicle->getUrl()]);
  234.                 } else {
  235.                     $link $this->router->generate('portal_new_car', ['dealer' => $vehicle->getDealer()->getUrl(), 'url' => $vehicle->getUrl(), 'variation'=> $vehicle->getVehicleItemId()]);
  236.                 }
  237.                 $items[] = [
  238.                     'preview' => $item->getPreview(),
  239.                     'vehicleType' => VehicleType::getTypeDataById($vehicle->getVehicleType()),
  240.                     'creditPayment' => $this->creditModel->getMinPayment($vehicle),
  241.                     'vehicle' => $vehicle,
  242.                     'link' => $link,
  243.                     'featuredId' => $featuresIds[$vehicle->getVehicleItemId()] ?? null,
  244.                     'comparedId' => $this->getComparison($vehicle),
  245.                     'vehiclePrice' => $vehicle->price(),
  246.                     'vehicleColors' => $this->getVehicleColors($vehicle),
  247.                     'hasNds' => false,
  248.                     'isSelect' => !in_array($vehicle->getDealer()->getId(), Catalog::NOT_VIDI_SELECT_DEALERS),
  249.                 ];
  250.             }
  251.         }
  252.         return count($items) > $limitRangeCount array_slice($items0,$limitRangeCount-1) : $items;
  253.     }
  254.     private function getVehicleColors($vehicle)
  255.     {
  256.         $vehicleColors = [];
  257.         foreach ($vehicle->getColors() as $color) {
  258.             if (!$color->getState()) {
  259.                 continue;
  260.             }
  261.             $imageVehicle $color->getGallery();
  262.             if (!$imageVehicle) {
  263.                 continue;
  264.             }
  265.             $firstImageVehicle $imageVehicle->getGalleryItems()->first();
  266.             if (!$firstImageVehicle) {
  267.                 continue;
  268.             }
  269.             $vehicleColors[$color->getId()] = [
  270.                 'imageColor' => $this->mediaExtension->getPath($color->getImage(), 'reference'),
  271.                 'imageWebpVehicle' => $this->mediaExtension->pathWebp($firstImageVehicle->getMedia(), 'reference'),
  272.                 'imageVehicle' => $this->mediaExtension->getPath($firstImageVehicle->getMedia(), 'reference'),
  273.             ];
  274.         }
  275.         return $vehicleColors;
  276.     }
  277. }