You are here

public function ParserService::downloadForecast in Weather 8

Same name and namespace in other branches
  1. 2.0.x src/Service/ParserService.php \Drupal\weather\Service\ParserService::downloadForecast()

Downloads a new forecast from yr.no.

Parameters

string $geoid: The GeoID for which the forecasts should be downloaded.

string $url: Full url of the forecast.

Return value

bool TRUE on success, FALSE on failure.

Throws

\Drupal\Core\Entity\EntityStorageException

1 call to ParserService::downloadForecast()
ParserService::downloadWeather in src/Service/ParserService.php
Downloads forecast from yr.no and puts it do DB.

File

src/Service/ParserService.php, line 128

Class

ParserService
ParserService service.

Namespace

Drupal\weather\Service

Code

public function downloadForecast(string $geoid = '', string $url = '') {

  // Specify timeout in seconds.
  $timeout = 10;
  if ($geoid) {
    $url = $this->weatherHelper
      ->getLinkForGeoid($geoid, 'yr');
  }

  /* @var $client \GuzzleHttp\Client */
  $client = $this->httpClient;
  try {
    $response = $client
      ->get($url, [
      'timeout' => $timeout,
    ]);

    // Extract XML data from the received forecast.
    return $this
      ->parseForecast($response
      ->getBody(), $geoid);
  } catch (RequestException $e) {

    // Make an entry about this error.
    $this->logger
      ->get('weather')
      ->error($this
      ->t('Download of forecast failed: @error', [
      '@error' => $e
        ->getMessage(),
    ]));

    // Show a message to users with administration privileges.
    if ($this->currentUser
      ->hasPermission('administer site configuration')) {
      $this->messenger
        ->addError($this
        ->t('Download of forecast failed: @error', [
        '@error' => $e
          ->getMessage(),
      ]));
    }
  }
}