src/Service/MateomaticsAgricultureService.php line 172

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use GuzzleHttp\Client;
  4. use DateTime;
  5. use App\Service\RedisCache;
  6. use GuzzleHttp;
  7. class MateomaticsAgricultureService
  8. {
  9.     private $apiBaseUrl MATEOMATICS_API_URL;
  10.     private $username;
  11.     private $password;
  12.     private $redisCache;
  13.     public function __construct(RedisCache $redisCache)
  14.     {
  15.         $this->username MATEOMATICS_API_USERNAME;
  16.         $this->password MATEOMATICS_API_PASSWORD;
  17.         $this->redisCache $redisCache;
  18.     }
  19.     public function getEvapotranspiration(array $coordinatesstring $datestring $interval '1h'$translator)
  20.     {
  21.         try {
  22.             if ($coordinates) {
  23.                 $latitude1 $coordinates[0][0];
  24.                 $longitude1 $coordinates[0][1];
  25.                 $latitude2 $coordinates[1][0];
  26.                 $longitude2 $coordinates[1][1];
  27.             }
  28.             // Validate the input parameters
  29.             if (!preg_match('/^[-]?[0-9]{1,2}\.[0-9]+/'$latitude1)) {
  30.                 return ["success" => false"message" => $translator->trans("invalid_latitude")];
  31.             }
  32.             if (!preg_match('/^[-]?[0-9]{1,3}\.[0-9]+/'$longitude1)) {
  33.                 return ["success" => false"message" => $translator->trans("invalid_longitude")];
  34.             }
  35.             // Validate the input parameters
  36.             if (!preg_match('/^[-]?[0-9]{1,2}\.[0-9]+/'$latitude2)) {
  37.                 return ["success" => false"message" => $translator->trans("invalid_latitude")];
  38.             }
  39.             if (!preg_match('/^[-]?[0-9]{1,3}\.[0-9]+/'$longitude2)) {
  40.                 return ["success" => false"message" => $translator->trans("invalid_longitude")];
  41.             }
  42.             if (empty($date)) {
  43.                 return ["success" => false"message" => $translator->trans("invalid_dates")];
  44.             }
  45.             $date date('Y-m-d\TH:i:s.v\Z', (strtotime($date)));
  46.             // Create a Redis key for the cache            
  47.             $cacheKey sprintf('evapotranspiration_%s_%s'md5(implode('_'array_map(function ($coordinate) {
  48.                 return implode('_'$coordinate);
  49.             }, $coordinates))), ($date));
  50.             // Try to get the data from Redis cache
  51.             $cachedData $this->redisCache->get($cacheKey);
  52.             if ($cachedData !== null) {
  53.                 // Return the cached data if available
  54.                 return $cachedData;
  55.             }
  56.             // If cache is empty, get the data from the API
  57.             $client = new Client(['verify' => false]);
  58.             $url sprintf(
  59.                 '%s/%s/evapotranspiration_%s:mm/%s,%s_%s,%s/json?use_decluttered=true',
  60.                 $this->apiBaseUrl,
  61.                 $date,
  62.                 $interval,
  63.                 $latitude1,
  64.                 $longitude1,
  65.                 $latitude2,
  66.                 $longitude2,
  67.             );
  68.             $response $client->request('GET'$url, [
  69.                 'auth' => [$this->username$this->password],
  70.             ]);
  71.             $statusCode $response->getStatusCode();
  72.             $data json_decode($response->getBody(), true);
  73.             // Set the data to Redis cache
  74.             $this->redisCache->set($cacheKey$data);
  75.             return $data;
  76.         } catch (GuzzleHttp\Exception\RequestException $e) {
  77.             return $e->getMessage();
  78.         } catch (\Exception $e) {
  79.             return $e->getMessage();
  80.         }
  81.     }
  82.     public function getGrowingDegreeDays(array $coordinatesstring $datestring $interval '1H'string $days '1')
  83.     {
  84.         try {
  85.             if ($coordinates) {
  86.                 $latitude $coordinates[0][0];
  87.                 $longitude $coordinates[0][1];
  88.             }
  89.             // Validate the input parameters
  90.             if (!preg_match('/^[-]?[0-9]{1,2}\.[0-9]+/'$latitude)) {
  91.                 throw new \InvalidArgumentException('Invalid latitude');
  92.             }
  93.             if (!preg_match('/^[-]?[0-9]{1,3}\.[0-9]+/'$longitude)) {
  94.                 throw new \InvalidArgumentException('Invalid longitude');
  95.             }
  96.             if (empty($date)) {
  97.                 throw new \InvalidArgumentException('Invalid dates');
  98.             }
  99.             $date date('Y-m-d\TH:i:s.v\Z', (strtotime($date)));
  100.             // Create a Redis key for the cache            
  101.             $cacheKey sprintf('growing_degree_days_accumulated_%s_%s'md5(implode('_'array_map(function ($coordinate) {
  102.                 return implode('_'$coordinate);
  103.             }, $coordinates))), ($date));
  104.             // Try to get the data from Redis cache
  105.             $cachedData $this->redisCache->get($cacheKey);
  106.             if ($cachedData !== null) {
  107.                 // Return the cached data if available
  108.                 return $cachedData;
  109.             }
  110.             // If cache is empty, get the data from the API
  111.             $client = new Client(['verify' => false]);
  112.             $url sprintf(
  113.                 '%s/%sP%sD:P%sD/growing_degree_days_accumulated:gdd/%s,%s/json?use_decluttered=true',
  114.                 $this->apiBaseUrl,
  115.                 $date,
  116.                 $interval,
  117.                 $days,
  118.                 $latitude,
  119.                 $longitude,
  120.             );
  121.             $response $client->request('GET'$url, [
  122.                 'auth' => [$this->username$this->password],
  123.             ]);
  124.             $statusCode $response->getStatusCode();
  125.             $data json_decode($response->getBody(), true);
  126.             // Set the data to Redis cache
  127.             $this->redisCache->set($cacheKey$data);
  128.             return $data;
  129.         } catch (GuzzleHttp\Exception\RequestException $e) {
  130.             return $e->getMessage();
  131.         } catch (\Exception $e) {
  132.             return $e->getMessage();
  133.         }
  134.     }
  135.     public function getGrassLandTemperatureSum(array $coordinatesstring $datestring $interval '1H'string $days '1'$translator)
  136.     {
  137.         try {
  138.             if ($coordinates) {
  139.                 $latitude $coordinates[0][0];
  140.                 $longitude $coordinates[0][1];
  141.             }
  142.             // Validate the input parameters
  143.             if (!preg_match('/^[-]?[0-9]{1,2}\.[0-9]+/'$latitude)) {
  144.                 return ["success" => false"message" => $translator->trans("invalid_latitude")];
  145.             }
  146.             if (!preg_match('/^[-]?[0-9]{1,3}\.[0-9]+/'$longitude)) {
  147.                 return ["success" => false"message" => $translator->trans("invalid_longitude")];
  148.             }
  149.             if (empty($date)) {
  150.                 return ["success" => false"message" => $translator->trans("invalid_dates")];
  151.             }
  152.             $date date('Y-m-d\TH:i:s.v\Z', (strtotime($date)));
  153.             // Create a Redis key for the cache            
  154.             $cacheKey sprintf('growing_degree_days_accumulated_%s_%s'md5(implode('_'array_map(function ($coordinate) {
  155.                 return implode('_'$coordinate);
  156.             }, $coordinates))), ($date));
  157.             // Try to get the data from Redis cache
  158.             $cachedData $this->redisCache->get($cacheKey);
  159.             if ($cachedData !== null) {
  160.                 // Return the cached data if available
  161.                 return $cachedData;
  162.             }
  163.             // If cache is empty, get the data from the API
  164.             $client = new Client(['verify' => false]);
  165.             $url sprintf(
  166.                 '%s/%sP%sD:PT%s/grass_land_temperature_sum:C,t_2m:C/%s,%s/json?use_decluttered=true',
  167.                 $this->apiBaseUrl,
  168.                 $date,
  169.                 $days,
  170.                 $interval,
  171.                 $latitude,
  172.                 $longitude
  173.             );
  174.             $response $client->request('GET'$url, [
  175.                 'auth' => [$this->username$this->password],
  176.             ]);
  177.             $statusCode $response->getStatusCode();
  178.             $data json_decode($response->getBody(), true);
  179.             // Set the data to Redis cache
  180.             $this->redisCache->set($cacheKey$data);
  181.             return $data;
  182.         } catch (GuzzleHttp\Exception\RequestException $e) {
  183.             return $e->getMessage();
  184.         } catch (\Exception $e) {
  185.             return $e->getMessage();
  186.         }
  187.     }
  188. }