src/Services/WeatherService.php line 110

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use App\Entity\Weather;
  4. use App\Repository\CompanyDetailsRepository;
  5. use App\Repository\WeatherRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. class WeatherService
  8. {
  9.     public function update()
  10.     {
  11.         $timeZone $this->companyDetailsRepository->find(1)->getCompanyTimeZone();
  12.         $lon $this->companyDetailsRepository->find(1)->getCompanyAddressLongitude();
  13.         $lat $this->companyDetailsRepository->find(1)->getCompanyAddressLatitude();
  14.         $location $this->companyDetailsRepository->find(1)->getWeatherLocation();
  15.         date_default_timezone_set($timeZone);
  16.         $api '13c3efa537a29520b090d83776daaa1b';
  17.         $url 'https://api.openweathermap.org/data/2.5/forecast?lat=' $lat '&lon=' $lon '&appid=' $api '&units=metric';
  18.         $jsonfile file_get_contents($url);
  19.         $jsondata json_decode($jsonfile);
  20.         if ($jsondata->cod == 200) {
  21.             foreach ($this->weatherRepository->findAll() as $weather) {
  22.                 $this->manager->remove($weather);
  23.                 $this->manager->flush();
  24.             }
  25.             foreach ($jsondata->list as $data) {
  26.                 $date_as_string date("Y-m-d H:i"$data->dt);
  27.                 $date = new \DateTime($date_as_string);
  28.                 $time date("H"$data->dt);
  29.                 $temperature $data->main->temp;
  30.                 $rain '';
  31.                 $rain_status $data->weather[0]->main;
  32.                 if ($rain_status == "Rain") {
  33.                     $rain $data->rain->{'3h'};
  34.                 }
  35.                 $weather = new Weather();
  36.                 $weather->setDate($date)
  37.                     ->setLocation($location)
  38.                     ->setTime($time)
  39.                     ->setWeather($temperature)
  40.                     ->setRain($rain);
  41.                 $this->manager->persist($weather);
  42.                 $this->manager->flush();
  43.             }
  44.         }
  45.         return null;
  46.     }
  47.     public function hourlyUpdate()
  48.     {
  49.         $timeZone $this->companyDetailsRepository->find(1)->getCompanyTimeZone();
  50.         $lon $this->companyDetailsRepository->find(1)->getCompanyAddressLongitude();
  51.         $lat $this->companyDetailsRepository->find(1)->getCompanyAddressLatitude();
  52.         $location $this->companyDetailsRepository->find(1)->getWeatherLocation();
  53.         date_default_timezone_set($timeZone);
  54.         $api '13c3efa537a29520b090d83776daaa1b';
  55.         $url 'https://api.openweathermap.org/data/3.0/onecall?lat=' $lat '&lon=' $lon '&exclude=current,minutely,daily,alert&appid=' $api '&units=metric';
  56.         $jsonfile file_get_contents($url);
  57.         $jsondata json_decode($jsonfile);
  58.         foreach ($this->weatherRepository->findAll() as $weather) {
  59.             $this->manager->remove($weather);
  60.             $this->manager->flush();
  61.         }
  62.         foreach ($jsondata->hourly as $data) {
  63.             $date_as_string date("Y-m-d H:i"$data->dt);
  64.             $date = new \DateTime($date_as_string);
  65.             $time date("H"$data->dt);
  66.             $temperature $data->temp;
  67.             $rain '';
  68.             $rain_status $data->weather[0]->main;
  69.             if ($rain_status == "Rain") {
  70.                 $rain $data->rain->{'1h'};
  71.             }
  72.             $weather = new Weather();
  73.             $weather->setDate($date)
  74.                 ->setLocation($location)
  75.                 ->setTime($time)
  76.                 ->setWeather($temperature)
  77.                 ->setRain($rain);
  78.             $this->manager->persist($weather);
  79.             $this->manager->flush();
  80.         }
  81.         return null;
  82.     }
  83.     public function getWeather(string $datetime)
  84.     {
  85.         // $date_as_string = $dateTime." ".$time;
  86.         $date = new \DateTime($datetime);
  87.         $weather $this->weatherRepository->findOneBy([
  88.             'date' => $date,
  89.             //'time' => $time
  90.         ]);
  91.         if ($weather) {
  92.             return $weather;
  93.         } else {
  94.             return null;
  95.         }
  96.     }
  97.     public function __construct(EntityManagerInterface $entityManagerWeatherRepository $weatherRepositoryCompanyDetailsRepository $companyDetailsRepository)
  98.     {
  99.         $this->manager $entityManager;
  100.         $this->weatherRepository $weatherRepository;
  101.         $this->companyDetailsRepository $companyDetailsRepository;
  102.     }
  103. }