HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/ijzerenman.komma.pro/app/Custom/Weather/WeatherService.php
<?php


namespace Komma\Weather;


use Carbon\Carbon;

class WeatherService
{
    /**
     * Contains decoded result from weather api
     *
     * @var \StdClass
     */
    private $weather;

    /**
     * @var WeatherApi
     */
    private $weatherApi;

    /**
     * CityId: Vught, NL
     *
     * @var string
     */
    private $cityId = '2745154';

    /**
     * @param WeatherApi $weatherApi
     */
    public function __construct(WeatherApi $weatherApi)
    {
        $this->weatherApi = $weatherApi;

        // Set weather variable on load
        $this->set();
    }

    /*
     * Returns the first full hour from now
     * f.e. if the time is 10:30 it returns 11:00
     */
    public function now()
    {
        if( ! isset($this->weather->list) || ! is_array($this->weather->list))
            return false;
        
        return array_first($this->weather->list, function()
        {
            // Find the first in the list where the time is larger than now
            return $this->weather->list > Carbon::now()->timestamp;
        });
    }

    /**
     * Return the current temperature
     *
     * @return bool
     */
    public function temperature()
    {
        if( ! $now = $this->now())
            return false;

        // Return temperature
        return $now->main->temp;
    }

    /**
     * Set the weather variable
     */
    private function set()
    {
        // Get last result from database
        $record = \DB::table('weather_api')->first();

        // Use record data if valid
        if($this->isValid($record))
        {
            // Use database record
            $jsonWeather = $record->weather;
        }
        else
        {
            // Make a new request
            $jsonWeather = $this->update();
        }

        // Set weather
        $this->weather = json_decode($jsonWeather);
    }

    /**
     * Get a new request from the API
     * Save this in the database
     * return Json result
     *
     * @return String
     */
    private function update()
    {
        // Get JSON from api
        $json = $this->request();

        // Update in database
        \DB::table('weather_api')
            ->take(1)
            ->update([
                'weather' => $json,
                'updated_at' => Carbon::now()
            ]);

        return $json;
    }

    /**
     * Make request to the api
     *
     * @return mixed
     */
    private function request()
    {
        return $this->weatherApi->request($this->cityId);
    }

    /**
     * Check if record is valid
     *
     * @param $record
     * @return bool
     */
    private function isValid($record)
    {
        // Weather should contain data
        if( ! isset($record->weather) || $record->weather == null ) return false;

        // Database record only lasts one hour
        $updated = Carbon::parse($record->updated_at);
        if($updated->addHour() < Carbon::now()) return false;

        return true;
    }
}