src/Controller/DefaultController.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\{HTTPStatusProd,HTTPStatusDev,GarageStatus,CarStockCebiaReportTableOldNewCRMXmlCheckFileCheck};
  4. use DateTime;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Console\Input\ArrayInput;
  11. use Symfony\Component\Console\Output\BufferedOutput;
  12. use Symfony\Bundle\FrameworkBundle\Console\Application;
  13. use Symfony\Component\HttpKernel\KernelInterface;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  15. use Symfony\Contracts\Cache\CacheInterface;
  16. use Symfony\Contracts\Cache\ItemInterface;
  17. class DefaultController extends AbstractController
  18. {
  19.     private string $projectDir;
  20.     public function __construct(ParameterBagInterface $params)
  21.     {
  22.         $this->projectDir $params->get('kernel.project_dir');
  23.     }
  24.     #[Route('/'name'default')]
  25.     public function index(): Response
  26.     {
  27.         return $this->render('default/index.html.twig', [
  28.             'oldvsnew' => OldNewCRM::getStatusCode($this->projectDir),
  29.             'xmlcheck' => XmlCheck::getStatusCode($this->projectDir),
  30.             'httpstatusprod' => HTTPStatusProd::getStatusCode($this->projectDir),
  31.             'httpstatusdev' => HTTPStatusDev::getStatusCode($this->projectDir),
  32.             'carstockstatus' => CarStock::getStatusCode($this->projectDir),
  33.             'garagestatus' => GarageStatus::getStatusCode($this->projectDir),
  34.             'filestatus' => FileCheck::getStatusCode($this->projectDir),
  35.             'xmlcheck_message' => XmlCheck::getStatusMessage($this->projectDir),
  36.             'carstockstatus_message' => CarStock::getStatusMessage($this->projectDir),
  37.             'garagestatus_message' => GarageStatus::getStatusMessage($this->projectDir)
  38.         ]);
  39.     }
  40.     #[Route('/file-check'name:'file_check')]
  41.     public function fileCheck(): Response
  42.     {
  43.         $file $this->getParameter('kernel.project_dir'). '/var/status/file-check.json';
  44.         if(!file_exists($file)) { return $this->redirectToRoute('default'); }
  45.         return $this->render('default/file_check.html.twig', [
  46.             'data' => json_decode(file_get_contents($file), true),
  47.             'last_update' => date('Y-m-d H:i:s'filemtime($file)),
  48.         ]);
  49.     }
  50.     #[Route('/old-vs-new'name'old_vs_new')]
  51.     public function oldVsNew(): Response
  52.     {
  53.         $file $this->getParameter('kernel.project_dir'). '/var/status/1_old_vs_new_crm.json';
  54.         if(!file_exists($file)) { return $this->redirectToRoute('default'); }
  55.         $oldvsnew json_decode(file_get_contents($file));
  56.         return $this->render('default/old_vs_new.html.twig', [
  57.             'oldvsnew' => $oldvsnew,
  58.             'last_update' => date('Y-m-d H:i:s'filemtime($file)),
  59.         ]);
  60.     }
  61.     #[Route('/xml-monitor'name'xml_monitor')]
  62.     public function xmlMonitor(): Response
  63.     {
  64.         $file $this->getParameter('kernel.project_dir'). '/var/status/2_xml_monitor.html';
  65.         if(!file_exists($file)){ return $this->redirectToRoute('default'); }
  66.         $data file_get_contents($file);
  67.         return $this->render('default/xml_check.html.twig', [
  68.             'data' => $data,
  69.             'last_update' => date('Y-m-d H:i:s'filemtime($file)),
  70.             'message' => XmlCheck::getStatusMessage($this->projectDir)
  71.         ]);
  72.     }
  73.     #[Route('/xml-monitor-group'name'xml_group')]
  74.     public  function xmlGroup(): Response
  75.     {
  76.         $file $this->getParameter('kernel.project_dir'). '/var/status/2_xml_monitor-group.html';
  77.         if(!file_exists($file)){ return $this->redirectToRoute('default'); }
  78.         $data file_get_contents($file);
  79.         return $this->render('default/xml_check_group.html.twig', [
  80.             'data' => $data,
  81.             'last_update' => date('Y-m-d H:i:s'filemtime($file))
  82.         ]);
  83.     }
  84.     #[Route('/json-monitor-group'name'json_group')]
  85.     public  function jsonGroup(): Response
  86.     {
  87.         $file $this->getParameter('kernel.project_dir'). '/var/status/2_json_monitor-group.html';
  88.         if(!file_exists($file)){ return $this->redirectToRoute('default'); }
  89.         $data file_get_contents($file);
  90.         return $this->render('default/json_check_group.html.twig', [
  91.             'data' => $data,
  92.             'last_update' => date('Y-m-d H:i:s'filemtime($file))
  93.         ]);
  94.     }
  95.     #[Route('/json-monitor'name'json_monitor')]
  96.     public function jsonMonitor(): Response
  97.     {
  98.         $file $this->getParameter('kernel.project_dir'). '/var/status/2_json_monitor.html';
  99.         if(!file_exists($file)){ return $this->redirectToRoute('default'); }
  100.         $data file_get_contents($file);
  101.         return $this->render('default/json_check.html.twig', [
  102.             'data' => $data,
  103.             'last_update' => date('Y-m-d H:i:s'filemtime($file)),
  104.             'message' => null,
  105.         ]);
  106.     }
  107.     #[Route('/http-status/prod'name'http_status_prod')]
  108.     public function httpStatusProd(): Response
  109.     {
  110.         $file $this->getParameter('kernel.project_dir'). '/var/status/http_status_prod.json';
  111.         if(!file_exists($file)){ return $this->redirectToRoute('default'); }
  112.         $jsonData json_decode(file_get_contents($file), true);
  113.         return $this->render('default/httpstatus_prod.html.twig', [
  114.             'data' => $jsonData,
  115.             'last_update' => date("Y-m-d H:i:s"filemtime($file))
  116.         ]);
  117.     }
  118.     #[Route('/http-status/dev'name'http_status_dev')]
  119.     public function httpStatusDev(): Response
  120.     {
  121.         $file $this->getParameter('kernel.project_dir'). '/var/status/http_status_dev.json';
  122.         if(!file_exists($file)){ return $this->redirectToRoute('default'); }
  123.         $jsonData json_decode(file_get_contents($file), true);
  124.         return $this->render('default/httpstatus_dev.html.twig', [
  125.             'data' => $jsonData,
  126.             'last_update' => date("Y-m-d H:i:s"filemtime($file))
  127.         ]);
  128.     }
  129.     #[Route('/car-stock'name'car_stock')]
  130.     public function carStock(): Response
  131.     {
  132.         $data $this->getParameter('kernel.project_dir') . '/var/status/car-stock.json';
  133.         $data2 $this->getParameter('kernel.project_dir') . '/var/status/car-stock-2.json';
  134.         $status $this->getParameter('kernel.project_dir') . '/var/status/car-stock-status.json';
  135.         if(!file_exists($data) OR !file_exists($status) OR !file_exists($data2)){ return $this->redirectToRoute('default'); }
  136.         return $this->render('default/car_stock.html.twig', [
  137.             'data' => json_decode(file_get_contents($data), true),
  138.             'data2' => json_decode(file_get_contents($data2), true),
  139.             'last_update' => date('Y-m-d H:i:s'filemtime($data)),
  140.             'message' => CarStock::getStatusMessage($this->projectDir)
  141.         ]);
  142.     }
  143.     #[Route('/garage-status'name'garage_status')]
  144.     public function garageStatus(): Response
  145.     {
  146.         $data $this->getParameter('kernel.project_dir') . '/var/status/garage_status_all.json';
  147.         if(!file_exists($data)){ return $this->redirectToRoute('default'); }
  148.         return $this->render('default/garage_status.html.twig', [
  149.             'data' => json_decode(file_get_contents($data), true),
  150.             'last_update' => date('Y-m-d H:i:s'filemtime($data)),
  151.             'message' => GarageStatus::getStatusMessage($this->projectDir)
  152.         ]);
  153.     }
  154.     #[Route('/cebia-report'name'cebia_report')]
  155.     public function cerbiaReport(CebiaReportTable $cebiaReportTableCacheInterface $cache): Response
  156.     {
  157.         /* $data = $this->getParameter('kernel.project_dir') . '/var/status/cebia-report.csv';
  158.         if(!file_exists($data)){ return $this->redirectToRoute('default'); }
  159.         $csvArray = [];
  160.         if(($handle = fopen($data, 'r')) !== false)
  161.         {
  162.             while (($data = fgetcsv($handle)) !== false)
  163.             {
  164.                 if (end($data) === '') {
  165.                     array_pop($data);
  166.                 }
  167.                 $csvArray[] = $data;
  168.             }
  169.             fclose($handle);
  170.         }
  171.         return $this->render('default/cebia_report.html.twig', [
  172.             'data' => $csvArray,
  173.             'last_update' => date('Y-m-d H:i:s', filemtime($data)),
  174.         ]); */
  175.         $htmlTable $cache->get('cebia_report_table', function (ItemInterface $item) use ($cebiaReportTable) {
  176.             $item->expiresAfter(3600);
  177.             return $cebiaReportTable->execute();
  178.         });
  179.         return $this->render('default/cebia_report.html.twig', [
  180.             'htmlTable' => $htmlTable,
  181.         ]);
  182.     }
  183.     #[Route('/exponea'name'app_exponea')]
  184.     public function exponea(): Response
  185.     {
  186.         $dataPath $this->getParameter('kernel.project_dir') . '/var/status/exponea.json';
  187.         if (!file_exists($dataPath)) { 
  188.             return $this->redirectToRoute('default'); 
  189.         }
  190.         $jsonData json_decode(file_get_contents($dataPath), true);
  191.         
  192.         return $this->render('default/exponea.html.twig', [
  193.             'data' => $jsonData,
  194.             'last_update' => (new \DateTime())->setTimestamp(filemtime($dataPath))->format('Y-m-d H:i:s'),
  195.         ]);
  196.     }
  197.     #[Route('/api/getLastUpdate/{name}')]
  198.     public function apiGetLastUpdate(string $name): JsonResponse
  199.     {
  200.         $projectDir $this->getParameter('kernel.project_dir');
  201.         $dataPath '';
  202.         switch ($name) {
  203.             case 'exponea':
  204.                 $dataPath $projectDir '/var/status/exponea.json';
  205.                 break;
  206.             case 'cebia-report':
  207.                 $dataPath $projectDir '/var/status/cebia-report.csv';
  208.                 break;
  209.             case 'garage_status_all':
  210.                 $dataPath $projectDir '/var/status/garage_status_all.json';
  211.                 break;
  212.             case 'car-stock':
  213.                 $dataPath $projectDir '/var/status/car-stock.json';
  214.                 break;
  215.             case 'http_status_dev':
  216.                 $dataPath $projectDir '/var/status/http_status_dev.json';
  217.                 break;
  218.             case 'http_status_prod':
  219.                 $dataPath $projectDir '/var/status/http_status_prod.json';
  220.                 break;
  221.             case 'xml_monitor':
  222.                 $dataPath $projectDir '/var/status/2_xml_monitor.html';
  223.                 break;
  224.             case 'old_vs_new':
  225.                 $dataPath $projectDir '/var/status/1_old_vs_new_crm.json';
  226.                 break;
  227.             case 'file_check':
  228.                 $dataPath $projectDir '/var/status/file-check.json';
  229.                 break;
  230.             default:
  231.                 return new JsonResponse(['error' => 'invalid name'], 400);
  232.         }
  233.         if (!file_exists($dataPath)) {
  234.             return new JsonResponse(['error' => 'file not found'], 404);
  235.         }
  236.         $lastUpdate date('Y-m-d H:i:s'filemtime($dataPath));
  237.         return new JsonResponse(['last_update' => $lastUpdate]);
  238.     }
  239.     #[Route('/database-sync'name'database_sync')]
  240.     public function databaseSync(): Response
  241.     {
  242.         $filePath $this->getParameter('kernel.project_dir') . '/var/status/database-sync-stats.json';
  243.         if (!file_exists($filePath)) {
  244.             return $this->redirectToRoute('default');
  245.         }
  246.         $data json_decode(file_get_contents($filePath), true);
  247.         $lastUpdate date('Y-m-d H:i:s'filemtime($filePath));
  248.         $brandMapping = [
  249.             => 'aaaauto',
  250.             => 'mototechna',
  251.             => 'auto-diskont'
  252.         ];
  253.         $countryMapping = [
  254.             23 => 'cs',
  255.             81 => 'pl',
  256.             92 => 'sk'
  257.         ];
  258.         $comparisonData = [];
  259.         foreach ($brandMapping as $brandId => $company) {
  260.             foreach ($countryMapping as $countryId => $countryCode) {
  261.                 $comparisonData[] = [
  262.                     'brand_id' => $brandId,
  263.                     'company' => $company,
  264.                     'country_id' => $countryId,
  265.                     'country_code' => $countryCode,
  266.                     'production' => $this->findCount($data['production'], $brandId$countryId),
  267.                     'resultmatic' => $this->findCount($data['resultmatic'], $brandId$countryId),
  268.                     'exponea' => $this->findCountExponea($data['exponea'], $company$countryCode)
  269.                 ];
  270.             }
  271.         }
  272.         return $this->render('default/database_sync.html.twig', [
  273.             'comparison_data' => $comparisonData,
  274.             'last_update' => $lastUpdate,
  275.         ]);
  276.     }
  277.     private function findCount(array $dataint $brandIdint $countryId): ?int
  278.     {
  279.         foreach ($data as $row) {
  280.             if ($row['brand_id'] == $brandId && $row['country_id'] == $countryId) {
  281.                 return (int) $row['count'];
  282.             }
  283.         }
  284.         return null;
  285.     }
  286.     private function findCountExponea(array $datastring $companystring $countryCode): ?int
  287.     {
  288.         foreach ($data as $row) {
  289.             if ($row['company'] == $company && $row['country_code'] == $countryCode) {
  290.                 return (int) $row['count'];
  291.             }
  292.         }
  293.         return null;
  294.     }
  295. }