src/Controller/WeatherController.php line 34

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\CompanyDetails;
  4. use App\Entity\Weather;
  5. use App\Form\WeatherType;
  6. use App\Repository\CompanyDetailsRepository;
  7. use App\Repository\WeatherRepository;
  8. use App\Services\CompanyDetailsService;
  9. use App\Services\WeatherService;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. /**
  16.  * @Route("/weather")
  17.  */
  18. class WeatherController extends AbstractController
  19. {
  20.     /**
  21.      * @Route("/index", name="weather_index", methods={"GET"})
  22.      */
  23.     public function index(Request $requestWeatherRepository $weatherRepositoryCompanyDetailsRepository $companyDetailsRepositoryWeatherService $weatherServiceEntityManagerInterface $entityManager): Response
  24.     {
  25.         $companyDetails $companyDetailsRepository->find('1');
  26.         if ($companyDetails) {
  27.             $lastScraped $companyDetails->getWeatherLastScraped();
  28.             $now = new \DateTimeImmutable();
  29.             $oneMinuteAgo $now->modify('-1 hour');
  30.             $needsRefresh false;
  31.             if ($lastScraped === null || $lastScraped $oneMinuteAgo) {
  32.                 $needsRefresh true;
  33.             }
  34.             if ($needsRefresh) {
  35.                 $weatherService->update();
  36.                 $companyDetails->setWeatherLastScraped($now);
  37.                 $entityManager->flush();
  38.             }
  39.         }
  40.         $today = new \DateTime('today');
  41.         $tomorrow = (clone $today)->modify('+1 day'); // (unused but harmless)
  42.         $dateParam $request->query->get('date');
  43.         $selectedDate null;
  44.         if ($dateParam) {
  45.             try {
  46.                 $selectedDate = new \DateTime($dateParam);
  47.             } catch (\Exception $e) {
  48.                 $selectedDate null;
  49.             }
  50.         }
  51.         $weather $weatherRepository->findFutureWeather($today$selectedDate);
  52.         $dates = [];
  53.         for ($i 0$i 4$i++) {
  54.             $current = (clone $today)->modify("+{$i} days");
  55.             $label $current->format('D, j M');
  56.             if ($i === 0) {
  57.                 $label .= ' (Today)';
  58.             } elseif ($i === 1) {
  59.                 $label .= ' (Tmrw)';
  60.             }
  61.             $dates[] = [
  62.                 'date' => $current,
  63.                 'label' => $label,
  64.             ];
  65.         }
  66.         return $this->render('weather/index.html.twig', [
  67.             'weather' => $weather,
  68.             'dates' => $dates,
  69.             'selectedDate' => $selectedDate $selectedDate->format('Y-m-d') : null,
  70.             'today' => $today,
  71.         ]);
  72.     }
  73.     /**
  74.      * @Route("/new", name="weather_new", methods={"GET","POST"})
  75.      */
  76.     public function new(Request $requestEntityManagerInterface $entityManager): Response
  77.     {
  78.         $weather = new Weather();
  79.         $form $this->createForm(WeatherType::class, $weather);
  80.         $form->handleRequest($request);
  81.         if ($form->isSubmitted() && $form->isValid()) {
  82.             $entityManager->persist($weather);
  83.             $entityManager->flush();
  84.             return $this->redirectToRoute('weather_index');
  85.         }
  86.         return $this->render('weather/new.html.twig', [
  87.             'weather' => $weather,
  88.             'form' => $form->createView(),
  89.         ]);
  90.     }
  91.     /**
  92.      * @Route("/show/{id}", name="weather_show", methods={"GET"})
  93.      */
  94.     public function show(Weather $weather): Response
  95.     {
  96.         return $this->render('weather/show.html.twig', [
  97.             'weather' => $weather,
  98.         ]);
  99.     }
  100.     /**
  101.      * @Route("/edit/{id}", name="weather_edit", methods={"GET","POST"})
  102.      */
  103.     public function edit(Request $requestWeather $weatherEntityManagerInterface $entityManager): Response
  104.     {
  105.         $form $this->createForm(WeatherType::class, $weather);
  106.         $form->handleRequest($request);
  107.         if ($form->isSubmitted() && $form->isValid()) {
  108.             $entityManager->flush();
  109.             return $this->redirectToRoute('weather_index');
  110.         }
  111.         return $this->render('weather/edit.html.twig', [
  112.             'weather' => $weather,
  113.             'form' => $form->createView(),
  114.         ]);
  115.     }
  116.     /**
  117.      * @Route("/delete/{id}", name="weather_delete", methods={"POST"})
  118.      */
  119.     public function delete(Request $requestWeather $weatherEntityManagerInterface $entityManager): Response
  120.     {
  121.         if ($this->isCsrfTokenValid('delete' $weather->getId(), $request->request->get('_token'))) {
  122.             $entityManager->remove($weather);
  123.             $entityManager->flush();
  124.         }
  125.         return $this->redirectToRoute('weather_index');
  126.     }
  127.     /**
  128.      * @Route("/weather/delete_all", name="weather_delete_all")
  129.      */
  130.     public function deleteAllWeatherData(WeatherRepository $weatherRepositoryEntityManagerInterface $entityManagerCompanyDetailsRepository $companyDetailsRepository)
  131.     {
  132.         $weathers $weatherRepository->findAll();
  133.         foreach ($weathers as $weather) {
  134.             $entityManager->remove($weather);
  135.         }
  136.         $companyDetails $companyDetailsRepository->findOneBy([]);
  137.         if ($companyDetails) {
  138.             $companyDetails->setWeatherLastScraped(null);
  139.         }
  140.         $entityManager->flush();
  141.         return $this->redirectToRoute('weather_index');
  142.     }
  143.     /**
  144.      * @Route("/weather/fetch_weather_data", name="fetch_weather_data", methods={"GET"})
  145.      */
  146.     public function weatherFetch(WeatherService $weatherServiceEntityManagerInterface $entityManagerRequest $request)
  147.     {
  148.         $referer $request->headers->get('Referer');
  149.         $weatherService->update();
  150.         $weatherService->hourlyUpdate();
  151.         $companyDetails $entityManager->getRepository(CompanyDetails::class)->find(1); // adjust if not ID 1
  152.         if ($companyDetails) {
  153.             $companyDetails->setWeatherLastScraped(new \DateTimeImmutable("now"));
  154.             $entityManager->flush();
  155.         }
  156.         return $this->redirect($referer);
  157.     }
  158. }