netgescon-day0/app/Support/AnnoGestioneContext.php

42 lines
1013 B
PHP

<?php
namespace App\Support;
use App\Models\User;
class AnnoGestioneContext
{
public const SESSION_KEY = 'netgescon.anno_gestione';
public static function resolveActiveAnno(?User $user = null): int
{
$sessionValue = session(self::SESSION_KEY);
if (is_numeric($sessionValue)) {
$year = (int) $sessionValue;
if ($year >= 2000 && $year <= 2100) {
return $year;
}
}
$default = (int) config('netgescon.defaults.default_anno_gestione', 0);
if ($default >= 2000 && $default <= 2100) {
session([self::SESSION_KEY => $default]);
return $default;
}
$year = (int) now()->year;
session([self::SESSION_KEY => $year]);
return $year;
}
public static function setActiveAnno(int $anno): bool
{
if ($anno < 2000 || $anno > 2100) {
return false;
}
session([self::SESSION_KEY => $anno]);
return true;
}
}