journal-intime/app/Services/HourService.php
Clement Desmidt bd12cf6dfb Ajoute la notification journalière
Il manque l'intégration du mail
Pour #13
2020-03-20 16:37:36 +01:00

83 lines
2.3 KiB
PHP

<?php
namespace App\Services;
use App\Exceptions\WrongHourException;
use App\User;
class HourService
{
const NO_NOTIFICATION = 0;
const MIDNIGHT_NOTIFICATION = 1;
const MORNING_NOTIFICATION = 2;
const NOON_NOTIFICATION = 3;
const EVENING_NOTIFICATION = 4;
public static $notifications_classes = [
self::NO_NOTIFICATION => 'slash',
self::MIDNIGHT_NOTIFICATION => 'moon',
self::MORNING_NOTIFICATION => 'sunrise',
self::NOON_NOTIFICATION => 'sun',
self::EVENING_NOTIFICATION => 'sunset',
];
/**
* @param User $user
* @return array
*/
public function getHoursBracket(User $user)
{
switch ((int)$user->notification_hour) {
case self::MIDNIGHT_NOTIFICATION:
return ['2100-2359', '0000-0600'];
break;
case self::MORNING_NOTIFICATION:
return ['0601-1100'];
break;
case self::NOON_NOTIFICATION:
return ['1101-1530'];
break;
case self::EVENING_NOTIFICATION:
return ['1531-2059'];
break;
case self::NO_NOTIFICATION:
default:
return [];
}
}
/**
* @param null|int|string $current_hour the hour and minute we want to find the id to. Formatted like `date('Hi')`
* @return int
* @throws WrongHourException
*/
public function getCurrentNotificationId($current_hour = null)
{
if (null === $current_hour) {
$current_hour = date('Hi');
}
$current_hour = (int) $current_hour;
if (
($current_hour >= 2100 && $current_hour < 2359) ||
($current_hour >= 0 && $current_hour < 600)
) {
return self::MIDNIGHT_NOTIFICATION;
}
if ($current_hour >= 600 && $current_hour < 1100) {
return self::MORNING_NOTIFICATION;
}
if ($current_hour >= 1100 && $current_hour < 1530) {
return self::NOON_NOTIFICATION;
}
if ($current_hour >= 1530 && $current_hour < 2100) {
return self::EVENING_NOTIFICATION;
}
throw new WrongHourException(sprintf('Given Time « %s » is not compatible', $current_hour));
}
}