src/Controller/InfoautoController.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Car;
  4. use App\Repository\InfoautoTokensRepository;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use App\Entity\InfoautoTokens;
  12. /**
  13.  * @Route("/infoauto")
  14.  */
  15. class InfoautoController extends AbstractController
  16. {
  17.     #[Route('/'name'infoauto_index')]
  18.     public function index(): Response
  19.     {
  20.         $dataJson json_decode(file_get_contents$this->getParameter('kernel.project_dir').'/public/brands-groups.json' ), true);
  21.         /*---- List brands [BEGIN] ----*/
  22.         $aBrands = [];
  23.         $aModels = [];
  24.         foreach ($dataJson as $brand) {
  25.             $aTemp = [];
  26.             $aTemp['brandId'] = $brand['id'];
  27.             $aTemp['brandName'] = $brand['name'];
  28.             $aTemp['brandListPrice'] = $brand['list_price'];
  29.             $aTemp['brandPrices'] = $brand['prices'];
  30.             $aTemp['brandPriceFrom'] = $brand['prices_from'];
  31.             $aTemp['brandPriceTo'] = $brand['prices_to'];
  32.             $aTemp['brandGroups'] = $brand['groups'];
  33.             /*foreach ($brand['groups'] as $group) {
  34.             }*/
  35.             $aBrands[] = $aTemp;
  36.         }
  37.         /*---- List brands [BEGIN] ----*/
  38.         //dump($aBrands); exit();
  39.         return $this->render('infoauto/index.html.twig', [
  40.             'currentMenu' => 'car',
  41.             'brands' => $aBrands,
  42.         ]);
  43.     }
  44.     #[Route('/getModal'name'infoauto_get_modal')]
  45.     public function getModal(): Response
  46.     {
  47.         $dataJson json_decode(file_get_contents$this->getParameter('kernel.project_dir').'/public/brands-groups.json' ), true);
  48.         /*---- List brands [BEGIN] ----*/
  49.         $aBrands = [];
  50.         $aModels = [];
  51.         foreach ($dataJson as $brand) {
  52.             $aTemp = [];
  53.             $aTemp['brandId'] = $brand['id'];
  54.             $aTemp['brandName'] = $brand['name'];
  55.             $aTemp['brandListPrice'] = $brand['list_price'];
  56.             $aTemp['brandPrices'] = $brand['prices'];
  57.             $aTemp['brandPriceFrom'] = $brand['prices_from'];
  58.             $aTemp['brandPriceTo'] = $brand['prices_to'];
  59.             $aTemp['brandGroups'] = $brand['groups'];
  60.             /*foreach ($brand['groups'] as $group) {
  61.             }*/
  62.             $aBrands[] = $aTemp;
  63.         }
  64.         /*---- List brands [BEGIN] ----*/
  65.         //dump($aBrands); exit();
  66.         return $this->render('car/get_infoauto_modal.html.twig', [
  67.             'currentMenu' => 'car',
  68.             'brands' => $aBrands,
  69.         ]);
  70.     }
  71.     /**
  72.      * @Route("/getVersions", name="infoauto_get_versions", methods={"GET"})
  73.      */
  74.     public function getVersions(ManagerRegistry $doctrineRequest $requestInfoautoTokensRepository $infoautoTokens): JsonResponse
  75.     {
  76.         $data['brandId'] = $request->get('brandId');
  77.         $data['modelId'] = $request->get('modelId');
  78.         $data['modelYear'] = $request->get('modelYear');
  79.         $token $this->getInfoautoToken($doctrine$infoautoTokens);
  80.         // Define los encabezados necesarios
  81.         $headers = [
  82.             'Authorization: Bearer '.$token,
  83.         ];
  84.         $url 'https://api.infoauto.com.ar/cars/pub//brands/'.$data['brandId'].'/groups/'.$data['modelId'].'/models?&prices=true&prices_from='.$data['modelYear'].'&prices_to='.$data['modelYear'].'&page_size=100';
  85.         if ($data['modelYear'] == '0km') {
  86.             $url 'https://api.infoauto.com.ar/cars/pub//brands/'.$data['brandId'].'/groups/'.$data['modelId'].'/models?&list_price=true&page_size=100';
  87.         }
  88.         /*---- Buscar modelos por año [BEGIN] ----*/
  89.         $curl curl_init();
  90.         curl_setopt_array($curl, array(
  91.             CURLOPT_URL => $url,
  92.             CURLOPT_RETURNTRANSFER => true,
  93.             CURLOPT_ENCODING => '',
  94.             CURLOPT_MAXREDIRS => 10,
  95.             CURLOPT_TIMEOUT => 0,
  96.             CURLOPT_FOLLOWLOCATION => true,
  97.             CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  98.             CURLOPT_CUSTOMREQUEST => 'GET',
  99.             CURLOPT_HTTPHEADER => $headers,   // Encabezados de la petición
  100.             CURLOPT_SSL_VERIFYPEER => false,
  101.             CURLOPT_SSL_VERIFYHOST => false,
  102.         ));
  103.         $response curl_exec($curl);
  104.         curl_close($curl);
  105.         // Verifica si ocurrió un error de cURL
  106.         if ($response === false) {
  107.             $error curl_error($curl);
  108.             curl_close($curl);
  109.             die("Error en cURL: $error");
  110.         }
  111.         // Obtiene el código HTTP de la respuesta
  112.         $httpCode curl_getinfo($curlCURLINFO_HTTP_CODE);
  113.         $modelsJson json_decode($responsetrue);
  114.         /*---- Buscar modelos por año [END] ----*/
  115.         //dump($modelsJson); exit();
  116.         /*---- Buscar precios [BEGIN] ----*/
  117.         // Recopilar CODIA
  118.         $aCodia null;
  119.         foreach ($modelsJson as $item) {
  120.             $aCodia[] = ['name' => $item['description'], 'codia' => $item['codia']];
  121.         }
  122.         if ($aCodia) {
  123.             // Buscar precios por CODIA
  124.             foreach ($aCodia as $key => $item) {
  125.                 $url 'https://api.infoauto.com.ar/cars/pub/models/'.$item['codia'].'/prices';
  126.                 if ($data['modelYear'] == '0km') {
  127.                     $url 'https://api.infoauto.com.ar/cars/pub/models/'.$item['codia'].'/list_price';
  128.                 }
  129.                 curl_setopt_array($curl, array(
  130.                     CURLOPT_URL => $url,
  131.                     CURLOPT_RETURNTRANSFER => true,
  132.                     CURLOPT_ENCODING => '',
  133.                     CURLOPT_MAXREDIRS => 10,
  134.                     CURLOPT_TIMEOUT => 0,
  135.                     CURLOPT_FOLLOWLOCATION => true,
  136.                     CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  137.                     CURLOPT_CUSTOMREQUEST => 'GET',
  138.                     CURLOPT_HTTPHEADER => $headers,   // Encabezados de la petición
  139.                     CURLOPT_SSL_VERIFYPEER => false,
  140.                     CURLOPT_SSL_VERIFYHOST => false,
  141.                 ));
  142.                 $response curl_exec($curl);
  143.                 curl_close($curl);
  144.                 // Verifica si ocurrió un error de cURL
  145.                 if ($response === false) {
  146.                     $error curl_error($curl);
  147.                     curl_close($curl);
  148.                     die("Error en cURL: $error");
  149.                 }
  150.                 $aPrices json_decode($responsetrue);
  151.                 $price 0;
  152.                 foreach ($aPrices as $item2) {
  153.                     if ($data['modelYear'] == '0km') {
  154.                         $price number_format($item20'''.');
  155.                         break;
  156.                     } else if ($item2['year'] == $data['modelYear']) {
  157.                         $price number_format($item2['price'], 0'''.');
  158.                         break;
  159.                     }
  160.                 }
  161.                 $aCodia[$key]['price'] = $price;
  162.             }
  163.             /*---- Buscar precios [END] ----*/
  164.         } else {
  165.             $data['status'] = 'error';
  166.             $data['type'] = 'version';
  167.             $data['message'] = 'No se encontró versión para el modelo';
  168.             echo '<h3>'.$data['message'].'</h3>';
  169.             exit();
  170.         }
  171.         //dump($aCodia); exit();
  172.         return $this->json($aCodia);
  173.     }
  174.     /**
  175.      * @Route("/getFeatures", name="infoauto_get_features", methods={"GET"})
  176.      */
  177.     public function getFeatures(ManagerRegistry $doctrineRequest $requestInfoautoTokensRepository $infoautoTokens): JsonResponse
  178.     {
  179.         $data['codia'] = $request->get('codia');
  180.         $token $this->getInfoautoToken($doctrine$infoautoTokens);
  181.         // Define los encabezados necesarios
  182.         $headers = [
  183.             'Authorization: Bearer '.$token,
  184.         ];
  185.         /*---- Buscar modelos por año [BEGIN] ----*/
  186.         $curl curl_init();
  187.         curl_setopt_array($curl, array(
  188.             CURLOPT_URL => 'https://api.infoauto.com.ar/cars/pub/models/'.$data['codia'].'/features/',
  189.             CURLOPT_RETURNTRANSFER => true,
  190.             CURLOPT_ENCODING => '',
  191.             CURLOPT_MAXREDIRS => 10,
  192.             CURLOPT_TIMEOUT => 0,
  193.             CURLOPT_FOLLOWLOCATION => true,
  194.             CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  195.             CURLOPT_CUSTOMREQUEST => 'GET',
  196.             CURLOPT_HTTPHEADER => $headers,   // Encabezados de la petición
  197.             CURLOPT_SSL_VERIFYPEER => false,
  198.             CURLOPT_SSL_VERIFYHOST => false,
  199.         ));
  200.         $response curl_exec($curl);
  201.         curl_close($curl);
  202.         // Verifica si ocurrió un error de cURL
  203.         if ($response === false) {
  204.             $error curl_error($curl);
  205.             curl_close($curl);
  206.             die("Error en cURL: $error");
  207.         }
  208.         // Obtiene el código HTTP de la respuesta
  209.         $httpCode curl_getinfo($curlCURLINFO_HTTP_CODE);
  210.         //$featuresJson = json_decode($response, true);
  211.         $featuresJson $response;
  212.         /*---- Buscar modelos por año [END] ----*/
  213.         //dump($featuresJson); exit();
  214.         return $this->json($featuresJson);
  215.     }
  216.     private function getInfoautoToken(ManagerRegistry $doctrineInfoautoTokensRepository $infoautoTokens)
  217.     {
  218.         $tokenTmp $infoautoTokens->findOneBy([], ['id' => 'DESC'], 1);
  219.         if ($tokenTmp) {
  220.             $hourMore = clone $tokenTmp->getCreatedAt();
  221.             $hourMore = ($hourMore)->add(new \DateInterval('PT1H'));
  222.             if (new \DateTime() > $hourMore) {
  223.                 $tokenOk false;
  224.             } else {
  225.                 $tokenOk true;
  226.             }
  227.         } else {
  228.             $tokenOk false;
  229.         }
  230.         if ($tokenOk) {
  231.             return $tokenTmp->getAccessToken();
  232.         } else {
  233.             $headers = [
  234.                 'Content-Type: application/json',
  235.                 'Authorization: Basic 'base64_encode('rwaybar@yahoo.com.ar:NQlv38cCxODlq42f'),
  236.                 'Content-Length: 0'
  237.             ];
  238.             $curl curl_init();
  239.             curl_setopt_array($curl, array(
  240.                 CURLOPT_URL => 'https://api.infoauto.com.ar/cars/auth/login',
  241.                 CURLOPT_RETURNTRANSFER => true,
  242.                 CURLOPT_ENCODING => '',
  243.                 CURLOPT_MAXREDIRS => 10,
  244.                 CURLOPT_TIMEOUT => 0,
  245.                 CURLOPT_FOLLOWLOCATION => true,
  246.                 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  247.                 CURLOPT_CUSTOMREQUEST => 'POST',
  248.                 CURLOPT_HTTPHEADER => $headers,   // Encabezados de la petición
  249.                 CURLOPT_SSL_VERIFYPEER => false,
  250.                 CURLOPT_SSL_VERIFYHOST => false,
  251.             ));
  252.             $response curl_exec($curl);
  253.             curl_close($curl);
  254.             // Verifica si ocurrió un error de cURL
  255.             if ($response === false) {
  256.                 $error curl_error($curl);
  257.                 curl_close($curl);
  258.                 die("Error en cURL: $error");
  259.             }
  260.             $json json_decode($response);
  261.             $tokens = new InfoautoTokens();
  262.             $tokens->setAccessToken($json->access_token);
  263.             $tokens->setRefreshToken($json->refresh_token);
  264.             $tokens->setCreatedAt(new \DateTime);
  265.             $doctrine->getManager()->persist($tokens);
  266.             $doctrine->getManager()->flush();
  267.             return $json->access_token;
  268.         }
  269.     }
  270. }