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/Notifications/NotificationService.php
<?php


namespace Komma\Notifications;


use Carbon\Carbon;
use Komma\Kms\Notifications\NotificationRepository;
use Komma\Weather\WeatherApi;
use Komma\Weather\WeatherService;

class NotificationService
{
    /**
     * @var NotificationRepository
     */
    private $repository;

    private $notifications;

    private $hours;

    /**
     * @var WeatherService
     */
    private $weatherService;

    /**
     * @param NotificationRepository $repository
     * @param WeatherService $weatherService
     */
    public function __construct(
        NotificationRepository $repository,
        WeatherService $weatherService
    )
    {
        $this->repository = $repository;
        $this->weatherService = $weatherService;
    }

    public function load()
    {
        $this->notifications = $this->repository->getById(1);
    }

    public function waterTemperature()
    {
        return $this->notifications->water_temperature;
    }

    public function weatherTemperature()
    {
        if($temperature = $this->weatherService->temperature())
        {
            $temperature = ceil($temperature);
        }
        else
        {
            $temperature = $this->notifications->weather_temperature;
        }

        return $temperature;
    }

    public function setOpeningHours()
    {

    }

    public function day()
    {
        // Display today
        $today = Carbon::today();
        $key = 'hours_' . \Str::lower($today->format('l'));
        if( ! empty($this->notifications->{$key}))
        {
            $this->hours = $this->notifications->{$key};
            return 'Vandaag geopend';
        }

        // Display tomorrow
        $tomorrow = Carbon::tomorrow();
        $key = 'hours_' . \Str::lower($tomorrow->format('l'));
        if( ! empty($this->notifications->{$key}))
        {
            $this->hours = $this->notifications->{$key};
            return 'Morgen geopend';
        }

        $nextDay = $tomorrow->addDay();

        for($i = 0; $i < 7; $i++)
        {
            if( $this->isClosed($nextDay))
            {
                $nextDay = $this->isClosed($nextDay);
            }
            else
            {
                break;
            }
        }

        // Display day
        $openDay = $nextDay->format('l');
        $key = 'hours_' . \Str::lower($openDay);
        $this->hours = $this->notifications->{$key};
        setlocale(LC_ALL, 'nl_NL');

        $dutchDay = $nextDay->formatLocalized('%A');

        return $dutchDay . ' geopend';
    }

    public function isClosed($day)
    {
        if(is_bool($day)) return $day;

        $key = 'hours_' . \Str::lower($day->format('l'));

        if( empty($this->notifications->{$key}) )
        {
            return $day->addDay(); // try next day
        }
        return $day; // done!
    }

    public function hours()
    {
        return $this->hours;
    }
}