You are here

private function ParserTest::weatherGetWeather in Weather 2.0.x

Same name and namespace in other branches
  1. 8 tests/src/Functional/ParserTest.php \Drupal\Tests\weather\Functional\ParserTest::weatherGetWeather()

Returns a weather object for the specified GeoID.

Parameters

string $geoid: GeoID of the place for which the weather is desired.

int $days: Return weather for specified number of days (0 = all available days).

bool $detailed: Return detailed forecasts or just one forecast per day.

Return value

array Weather array with forecast information.

Throws

\Exception

2 calls to ParserTest::weatherGetWeather()
ParserTest::getInfoAboutForecast in tests/src/Functional/ParserTest.php
Internal helper function for getting information about a forecast.
ParserTest::testDifferentDaysOfForecasts in tests/src/Functional/ParserTest.php
Test the parser with different days of forecast data.

File

tests/src/Functional/ParserTest.php, line 401

Class

ParserTest
Tests parsing of XML weather forecasts.

Namespace

Drupal\Tests\weather\Functional

Code

private function weatherGetWeather($geoid, $days = 0, $detailed = TRUE) {

  // Support testing of module with fixed times instead of current time.
  $config = \Drupal::configFactory()
    ->getEditable('weather.settings');
  $time = $config
    ->get('weather_time_for_testing');

  // Get the scheduled time of next update. If there is no entry for
  // the specified GeoID, $meta will be FALSE.
  $query = \Drupal::database()
    ->select('weather_forecast_information', 'wfi');
  $query
    ->fields('wfi', [
    'geoid',
  ]);
  $query
    ->condition('wfi.geoid', $geoid, '=');
  $meta = $query
    ->execute();
  $meta = $meta
    ->fetchAssoc();
  $current_utc_time = gmdate('Y-m-d H:i:s', $time);

  // If the next scheduled download is due, try to get forecasts.
  if (!empty($meta)) {
    $meta['next_download_attempt'] = $time;
  }
  if ($meta === FALSE or $current_utc_time >= $meta['next_download_attempt']) {
    $result = $this
      ->weatherDownloadForecast($geoid);

    // If that worked, get the next scheduled update time.
    $query = \Drupal::database()
      ->select('weather_forecast_information', 'wfi');
    $query
      ->fields('wfi', [
      'geoid',
    ]);
    $query
      ->condition('wfi.geoid', $geoid, '=');
    $meta = $query
      ->execute();
    $meta = $meta
      ->fetchAssoc();

    // If there is no entry yet, set up initial values.
    if ($meta === FALSE) {
      $meta['geoid'] = $geoid;
      $meta['last_update'] = $current_utc_time;
      $meta['next_update'] = $current_utc_time;
      $meta['next_download_attempt'] = $current_utc_time;
      $meta['utc_offset'] = 120;
    }
    if (empty($meta['next_update'])) {
      $meta['next_update'] = $time;
    }

    // The second check is needed if the download did succeed, but
    // the returned forecast is old and the next update is overdue.
    // In that case, the download attempt needs to wait as well,
    // otherwise, a new download will occur on every page load.
    if ($result == FALSE || $current_utc_time >= $meta['next_update']) {

      // The download did not succeed. Set next download attempt accordingly.
      // Calculate the UTC timestamp.
      $next_update = strtotime($meta['next_update'] . ' UTC');

      // Initial retry after 675 seconds (11.25 minutes).
      // This way, the doubling on the first day leads to exactly 86400
      // seconds (one day) update interval.
      $seconds_to_retry = 675;
      while ($next_update + $seconds_to_retry <= $time) {
        if ($seconds_to_retry < 86400) {
          $seconds_to_retry = $seconds_to_retry * 2;
        }
        else {
          $seconds_to_retry = $seconds_to_retry + 86400;
        }
      }

      // Finally, calculate the UTC time of the next download attempt.
      $meta['next_download_attempt'] = gmdate('Y-m-d H:i:s', $next_update + $seconds_to_retry);
      \Drupal::database()
        ->delete('weather_forecast_information')
        ->condition('geoid', $meta['geoid'])
        ->execute();

      // Write new entry.
      \Drupal::database()
        ->insert('weather_forecast_information')
        ->fields($meta)
        ->execute();
    }
  }
  if (empty($meta['utc_offset'])) {
    $meta['utc_offset'] = 120;
  }
  $return_array['forecasts'] = $this
    ->weatherGetForecastsFromDatabase($geoid, $meta['utc_offset'], $days, $detailed, $time);
  $return_array['utc_offset'] = $meta['utc_offset'];
  return $return_array;
}