You are here

public function ParserService::downloadForecast in Weather 2.0.x

Same name and namespace in other branches
  1. 8 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 129

Class

ParserService
Parsing of XML weather forecasts from yr.no.

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');
  }
  $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(),
      ]));
    }
  }
}