openWeather = $openWeather; $this->em = $em; } /** * @Route("/", name="index", methods={"GET"}) */ public function index() { $weather = new Weather(); $form = $this->createForm(WeatherType::class, $weather); return $this->render('map/index.html.twig', [ 'form' => $form->createView(), ]); } /** * @Route("get-weather", name="getWeather", methods={"POST"}) * @param Request $request * @return JsonResponse * @throws ClientExceptionInterface * @throws DecodingExceptionInterface * @throws RedirectionExceptionInterface * @throws ServerExceptionInterface * @throws TransportExceptionInterface */ public function getWeather(Request $request) { if ($request->isXMLHttpRequest()) { $lat = $request->request->get('weather')['lat']; $lng = $request->request->get('weather')['lng']; if (!empty($lng) && !empty($lat)) { $form = $this->createForm(WeatherType::class); $form->handleRequest($request); if ($form->isValid()) { $weather = $this->openWeather->fetchCurrentWeatherByCoordinates($lat, $lng); if ($weather) { $this->saveResults($weather); return new JsonResponse($weather); } else { return new JsonResponse(['status' => 'error', 'message' => 'Open Weather Map error']); } } else { return new JsonResponse($form->getErrors()); } } return new JsonResponse(['status' => 'error', 'message' => 'Error']); } } /** * @Route("results", name="results", methods={"GET"}) * @param PaginatorInterface $paginator * @param Request $request * @param WeatherRepository $repository * @return Response */ public function results(PaginatorInterface $paginator, Request $request, WeatherRepository $repository) { $results = $repository->paginationList(); $pagination = $paginator->paginate( $results, /* query NOT result */ $request->query->getInt('page', 1), /*page number*/ Weather::PAGE_LIMIT /*limit per page*/ ); return $this->render('map/results.html.twig', [ 'pagination' => $pagination, 'temperatureStats' => $repository->findTemperatureStats(), 'commonCity' => $repository->findCommonCity() ]); } private function saveResults($result) { $city = $this->em->getRepository(City::class)->findOneBy(['openWeatherId' => $result['id']]); if (empty($city)) { $city = new City(); $city->setName($result['name']); $city->setOpenWeatherId($result['id']); $this->em->persist($city); } $weather = new Weather(); $weather->setLat($result['coord']['lat']); $weather->setLng($result['coord']['lon']); $weather->setDescription($result['weather'][0]['description']); $weather->setCloudiness($result['clouds']['all']); $weather->setTemperature($result['main']['temp']); $weather->setWindSpeed($result['wind']['speed']); $weather->setCity($city); $this->em->persist($weather); $this->em->flush(); } }