You are here

class LiveWeather in Live Weather 8

Same name and namespace in other branches
  1. 8.2 src/LiveWeather.php \Drupal\live_weather\LiveWeather

Live weather.

Hierarchy

Expanded class hierarchy of LiveWeather

1 string reference to 'LiveWeather'
live_weather.services.yml in ./live_weather.services.yml
live_weather.services.yml
1 service uses LiveWeather
live_weather.controller in ./live_weather.services.yml
Drupal\live_weather\LiveWeather

File

src/LiveWeather.php, line 18
Contains \Drupal\live_weather\LiveWeather.

Namespace

Drupal\live_weather
View source
class LiveWeather implements LiveWeatherInterface {

  /**
   * The HTTP client to fetch the feed data with.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $httpClient;

  /**
   * A logger instance.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * Constructs a location form object.
   *
   * @param \GuzzleHttp\ClientInterface $http_client
   *   The Guzzle HTTP client.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   */
  public function __construct(ClientInterface $http_client, LoggerInterface $logger) {
    $this->httpClient = $http_client;
    $this->logger = $logger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('http_client'), $container
      ->get('logger.factory'));
  }

  /**
   * Get location data.
   */
  public function locationCheck($woeid = NULL, $filter = '', $unit = 'f') {
    $data = '';
    $url = 'https://query.yahooapis.com/v1/public/yql?q=select ' . $filter . ' from weather.forecast where woeid IN (' . $woeid . ') and u= "' . $unit . '"&format=json';
    try {
      $response = $this->httpClient
        ->get($url);
      $data = (string) $response
        ->getBody();
    } catch (RequestException $e) {
      $this->logger
        ->warning('Failed to get data "%error".', array(
        '%error' => $e
          ->getMessage(),
      ));
      return;
    } catch (BadResponseException $e) {
      $this->logger
        ->warning('Failed to get data "%error".', array(
        '%error' => $e
          ->getMessage(),
      ));
      return;
    } catch (\Exception $e) {
      $this->logger
        ->warning('Failed to get data "%error".', array(
        '%error' => $e
          ->getMessage(),
      ));
      return;
    }
    if (!empty($data)) {
      $data = Json::decode($data);
    }
    return $data;
  }

  /**
   * Check Day or Night.
   */
  public static function checkDayNight($date, $sunrise, $sunset) {
    $position = Unicode::strpos($date, ":");
    $tpb = Unicode::substr($date, $position - 2, 8);
    $actual_time = strtotime($tpb);
    $sunrise_time = strtotime($sunrise);
    $sunset_time = strtotime($sunset);
    if ($actual_time > $sunrise_time && $actual_time < $sunset_time) {
      return 'd';
    }
    else {
      return 'n';
    }
    return 'd';
  }

  /**
   * Get Wind Direction.
   */
  public static function windDirection($direction) {
    if ($direction >= 348.75 && $direction <= 360) {
      $direction = "N";
    }
    elseif ($direction >= 0 && $direction < 11.25) {
      $direction = "N";
    }
    elseif ($direction >= 11.25 && $direction < 33.75) {
      $direction = "NNE";
    }
    elseif ($direction >= 33.75 && $direction < 56.25) {
      $direction = "NE";
    }
    elseif ($direction >= 56.25 && $direction < 78.75) {
      $direction = "ENE";
    }
    elseif ($direction >= 78.75 && $direction < 101.25) {
      $direction = "E";
    }
    elseif ($direction >= 101.25 && $direction < 123.75) {
      $direction = "ESE";
    }
    elseif ($direction >= 123.75 && $direction < 146.25) {
      $direction = "SE";
    }
    elseif ($direction >= 146.25 && $direction < 168.75) {
      $direction = "SSE";
    }
    elseif ($direction >= 168.75 && $direction < 191.25) {
      $direction = "S";
    }
    elseif ($direction >= 191.25 && $direction < 213.75) {
      $direction = "SSW";
    }
    elseif ($direction >= 213.75 && $direction < 236.25) {
      $direction = "SW";
    }
    elseif ($direction >= 236.25 && $direction < 258.75) {
      $direction = "WSW";
    }
    elseif ($direction >= 258.75 && $direction < 281.25) {
      $direction = "W";
    }
    elseif ($direction >= 281.25 && $direction < 303.75) {
      $direction = "WNW";
    }
    elseif ($direction >= 303.75 && $direction < 326.25) {
      $direction = "NW";
    }
    elseif ($direction >= 326.25 && $direction < 348.75) {
      $direction = "NNW";
    }
    return $direction;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LiveWeather::$httpClient protected property The HTTP client to fetch the feed data with.
LiveWeather::$logger protected property A logger instance.
LiveWeather::checkDayNight public static function Check Day or Night. Overrides LiveWeatherInterface::checkDayNight
LiveWeather::create public static function
LiveWeather::locationCheck public function Get location data. Overrides LiveWeatherInterface::locationCheck
LiveWeather::windDirection public static function Get Wind Direction. Overrides LiveWeatherInterface::windDirection
LiveWeather::__construct public function Constructs a location form object.