X-Git-Url: https://git.rapsys.eu/.gitweb.cgi/airbundle/blobdiff_plain/f0ed7af138b95f31354f8d5a37cb4cd27b902a75..d3c8ab5a395adc36ec3095e7effb6f0db195887b:/Entity/Session.php?ds=sidebyside diff --git a/Entity/Session.php b/Entity/Session.php index 084580e..1cba677 100644 --- a/Entity/Session.php +++ b/Entity/Session.php @@ -11,142 +11,116 @@ namespace Rapsys\AirBundle\Entity; +use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Event\PreUpdateEventArgs; +use Rapsys\PackBundle\Util\IntlUtil; + /** * Session */ class Session { /** - * @var integer - */ - private $id; - - /** - * @var \DateTime - */ - private $date; - - /** - * @var \DateTime + * Primary key */ - private $begin; + private ?int $id = null; /** - * @var \DateTime + * Begin time */ - private $start; + private ?\DateTime $begin = null; /** - * @var \DateTime + * Computed start datetime */ - private $length; + private ?\DateTime $start = null; /** - * @var \DateTime + * Length time */ - private $stop; + private ?\DateTime $length = null; /** - * @var boolean + * Computed stop datetime */ - private $premium; + private ?\DateTime $stop = null; /** - * @var float + * Premium */ - private $rainfall; + private ?bool $premium = null; /** - * @var float + * Rain mm */ - private $rainrisk; + private ?float $rainfall = null; /** - * @var float + * Rain chance */ - private $realfeel; + private ?float $rainrisk = null; /** - * @var float + * Real feel temperature */ - private $realfeelmin; + private ?float $realfeel = null; /** - * @var float + * Real feel minimum temperature */ - private $realfeelmax; + private ?float $realfeelmin = null; /** - * @var float + * Real feel maximum temperature */ - private $temperature; + private ?float $realfeelmax = null; /** - * @var float + * Temperature */ - private $temperaturemin; + private ?float $temperature = null; /** - * @var float + * Minimum temperature */ - private $temperaturemax; + private ?float $temperaturemin = null; /** - * @var \DateTime + * Maximum temperature */ - private $locked; + private ?float $temperaturemax = null; /** - * @var \DateTime + * Lock datetime */ - private $created; + private ?\DateTime $locked = null; /** - * @var \DateTime + * Creation datetime */ - private $updated; + private \DateTime $created; /** - * @var Application + * Update datetime */ - private $application; + private \DateTime $updated; /** - * @var Location + * Application instance */ - private $location; + private ?Application $application = null; /** - * @var Slot + * Applications collection */ - private $slot; - - /** - * @var ArrayCollection - */ - private $applications; + private Collection $applications; /** * Constructor */ - public function __construct() { + public function __construct(private \DateTime $date, private Location $location, private Slot $slot) { //Set defaults - $this->begin = null; - $this->start = null; - $this->length = null; - $this->stop = null; - $this->premium = null; - $this->rainfall = null; - $this->rainrisk = null; - $this->realfeel = null; - $this->realfeelmin = null; - $this->realfeelmax = null; - $this->temperature = null; - $this->temperaturemin = null; - $this->temperaturemax = null; - $this->locked = null; $this->created = new \DateTime('now'); $this->updated = new \DateTime('now'); $this->applications = new ArrayCollection(); @@ -157,7 +131,7 @@ class Session { * * @return integer */ - public function getId(): int { + public function getId(): ?int { return $this->id; } @@ -285,11 +259,11 @@ class Session { /** * Set premium * - * @param boolean $premium + * @param bool $premium * * @return Session */ - public function setPremium(bool $premium): Session { + public function setPremium(?bool $premium): Session { $this->premium = $premium; return $this; @@ -300,7 +274,7 @@ class Session { * * @return bool */ - public function getPremium(): bool { + public function getPremium(): ?bool { return $this->premium; } @@ -628,7 +602,7 @@ class Session { * * @return Session */ - public function setApplication(Application $application): Session { + public function setApplication(?Application $application): Session { $this->application = $application; return $this; @@ -648,7 +622,7 @@ class Session { */ public function preUpdate(PreUpdateEventArgs $eventArgs) { //Check that we have a session instance - if (($session = $eventArgs->getEntity()) instanceof Session) { + if (($session = $eventArgs->getObject()) instanceof Session) { //Set updated value $session->setUpdated(new \DateTime('now')); } @@ -660,6 +634,8 @@ class Session { * Consider as premium a day off for afternoon, the eve for evening and after * Store computed result in premium member for afternoon and evening * + * @TODO improve by moving day off computation in IntlUtil or HolidayUtil class ? + * * @return bool Whether the date is day off or not */ public function isPremium(): bool { @@ -730,7 +706,7 @@ class Session { } //Get eastern - $eastern = $this->getEastern($date->format('Y')); + $eastern = (new IntlUtil())->getEastern($date->format('Y')); //Check dynamic holidays if ( @@ -757,48 +733,4 @@ class Session { //Date is not a holiday and week day return false; } - - /** - * Compute eastern for selected year - * - * @param string $year The eastern year - * - * @return DateTime The eastern date - */ - private function getEastern(string $year): \DateTime { - //Set static - static $data = null; - - //Check if already computed - if (isset($data[$year])) { - //Return computed eastern - return $data[$year]; - //Check if data is null - } elseif (is_null($data)) { - //Init data array - $data = []; - } - - $d = (19 * ($year % 19) + 24) % 30; - - $e = (2 * ($year % 4) + 4 * ($year % 7) + 6 * $d + 5) % 7; - - $day = 22 + $d + $e; - - $month = 3; - - if ($day > 31) { - $day = $d + $e - 9; - $month = 4; - } elseif ($d == 29 && $e == 6) { - $day = 10; - $month = 4; - } elseif ($d == 28 && $e == 6) { - $day = 18; - $month = 4; - } - - //Store eastern in data - return ($data[$year] = new \DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day))); - } }