Facebook
From Unreliable Monkey, 3 Years ago, written in PHP.
Embed
Download Paste or View Raw
Hits: 527
  1. <?php
  2.  
  3.  
  4. namespace App\Controller;
  5.  
  6. use App\Entity\City;
  7. use App\Entity\Weather;
  8. use App\Repository\WeatherRepository;
  9. use App\Service\OpenWeather;
  10. use App\Type\WeatherType;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Knp\Component\Pager\PaginatorInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  19. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  20. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  21. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  22. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  23.  
  24. /**
  25.  * Contactsubject controller.
  26.  *
  27.  * @Route("/")
  28.  */
  29. class MapController extends AbstractController
  30. {
  31.     private $openWeather;
  32.     private $em;
  33.  
  34.     public function __construct(OpenWeather $openWeather, EntityManagerInterface $em)
  35.     {
  36.         $this->openWeather = $openWeather;
  37.         $this->em = $em;
  38.     }
  39.  
  40.     /**
  41.      * @Route("/", name="index", methods={"GET"})
  42.      */
  43.     public function index()
  44.     {
  45.         $weather = new Weather();
  46.         $form = $this->createForm(WeatherType::class, $weather);
  47.  
  48.         return $this->render('map/index.html.twig', [
  49.             'form' => $form->createView(),
  50.         ]);
  51.     }
  52.  
  53.     /**
  54.      * @Route("get-weather", name="getWeather", methods={"POST"})
  55.      * @param Request $request
  56.      * @return JsonResponse
  57.      * @throws ClientExceptionInterface
  58.      * @throws DecodingExceptionInterface
  59.      * @throws RedirectionExceptionInterface
  60.      * @throws ServerExceptionInterface
  61.      * @throws TransportExceptionInterface
  62.      */
  63.     public function getWeather(Request $request)
  64.     {
  65.         if ($request->isXMLHttpRequest()) {
  66.  
  67.             $lat = $request->request->get('weather')['lat'];
  68.             $lng = $request->request->get('weather')['lng'];
  69.  
  70.             if (!empty($lng) && !empty($lat)) {
  71.  
  72.                 $form = $this->createForm(WeatherType::class);
  73.                 $form->handleRequest($request);
  74.  
  75.                 if ($form->isValid()) {
  76.                     $weather = $this->openWeather->fetchCurrentWeatherByCoordinates($lat, $lng);
  77.  
  78.                     if ($weather) {
  79.  
  80.                         $this->saveResults($weather);
  81.  
  82.                         return new JsonResponse($weather);
  83.                     } else {
  84.                         return new JsonResponse(['status' => 'error', 'message' => 'Open Weather Map error']);
  85.                     }
  86.                 } else {
  87.                     return new JsonResponse($form->getErrors());
  88.                 }
  89.             }
  90.  
  91.             return new JsonResponse(['status' => 'error', 'message' => 'Error']);
  92.         }
  93.     }
  94.  
  95.     /**
  96.      * @Route("results", name="results", methods={"GET"})
  97.      * @param PaginatorInterface $paginator
  98.      * @param Request $request
  99.      * @param WeatherRepository $repository
  100.      * @return Response
  101.      */
  102.     public function results(PaginatorInterface $paginator, Request $request, WeatherRepository $repository)
  103.     {
  104.         $results = $repository->paginationList();
  105.  
  106.         $pagination = $paginator->paginate(
  107.             $results, /* query NOT result */
  108.             $request->query->getInt('page', 1), /*page number*/
  109.             Weather::PAGE_LIMIT /*limit per page*/
  110.         );
  111.  
  112.         return $this->render('map/results.html.twig', [
  113.             'pagination' => $pagination,
  114.             'temperatureStats' => $repository->findTemperatureStats(),
  115.             'commonCity' => $repository->findCommonCity()
  116.         ]);
  117.     }
  118.  
  119.     private function saveResults($result)
  120.     {
  121.         $city = $this->em->getRepository(City::class)->findOneBy(['openWeatherId' => $result['id']]);
  122.  
  123.         if (empty($city)) {
  124.             $city = new City();
  125.             $city->setName($result['name']);
  126.             $city->setOpenWeatherId($result['id']);
  127.  
  128.             $this->em->persist($city);
  129.         }
  130.  
  131.         $weather = new Weather();
  132.         $weather->setLat($result['coord']['lat']);
  133.         $weather->setLng($result['coord']['lon']);
  134.         $weather->setDescription($result['weather'][0]['description']);
  135.         $weather->setCloudiness($result['clouds']['all']);
  136.         $weather->setTemperature($result['main']['temp']);
  137.         $weather->setWindSpeed($result['wind']['speed']);
  138.         $weather->setCity($city);
  139.  
  140.         $this->em->persist($weather);
  141.  
  142.         $this->em->flush();
  143.     }
  144. }