]> Raphaël G. Git Repositories - airbundle/blobdiff - Entity/Session.php
Version 0.6.1
[airbundle] / Entity / Session.php
index 70b2fc4bed5e92bb2855c4b65f4aa91cc08f65b0..8d897690017d51d506d3eaadd0787bf9a8d218e3 100644 (file)
 <?php declare(strict_types=1);
 
 /*
 <?php declare(strict_types=1);
 
 /*
- * this file is part of the rapsys packbundle package.
+ * This file is part of the Rapsys AirBundle package.
  *
  *
- * (c) raphaël gertz <symfony@rapsys.eu>
+ * (c) Raphaël Gertz <symfony@rapsys.eu>
  *
  *
- * for the full copyright and license information, please view the license
+ * For the full copyright and license information, please view the LICENSE
  * file that was distributed with this source code.
  */
 
 namespace Rapsys\AirBundle\Entity;
 
  * file that was distributed with this source code.
  */
 
 namespace Rapsys\AirBundle\Entity;
 
+use Doctrine\Common\Collections\Collection;
 use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\ORM\Event\PreUpdateEventArgs;
 
 use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\ORM\Event\PreUpdateEventArgs;
 
+use Rapsys\PackBundle\Util\IntlUtil;
+
 /**
  * Session
  */
 class Session {
        /**
 /**
  * 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 ?\DateTimeImmutable $begin = null;
 
        /**
 
        /**
-        * @var \DateTime
+        * Computed start datetime
         */
         */
-       private $length;
+       private ?\DateTimeImmutable $start = null;
 
        /**
 
        /**
-        * @var \DateTime
+        * Length time
         */
         */
-       private $stop;
+       private ?\DateTimeImmutable $length = null;
 
        /**
 
        /**
-        * @var bool
+        * Computed stop datetime
         */
         */
-       private $premium;
+       private ?\DateTimeImmutable $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 ?\DateTimeImmutable $locked = null;
 
        /**
 
        /**
-        * @var \DateTime
+        * Create datetime
         */
         */
-       private $updated;
+       private \DateTimeImmutable $created;
 
        /**
 
        /**
-        * @var Application
+        * Update datetime
         */
         */
-       private $application;
+       private \DateTimeImmutable $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
         */
 
        /**
         * Constructor
         */
-       public function __construct() {
+       public function __construct(private \DateTimeImmutable $date, private Location $location, private Slot $slot) {
                //Set defaults
                //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->application = null;
+               $this->created = new \DateTimeImmutable('now');
+               $this->updated = new \DateTimeImmutable('now');
+
+               //Set collections
                $this->applications = new ArrayCollection();
        }
 
                $this->applications = new ArrayCollection();
        }
 
@@ -158,18 +133,18 @@ class Session {
         *
         * @return integer
         */
         *
         * @return integer
         */
-       public function getId(): int {
+       public function getId(): ?int {
                return $this->id;
        }
 
        /**
         * Set date
         *
                return $this->id;
        }
 
        /**
         * Set date
         *
-        * @param \DateTime $date
+        * @param \DateTimeImmutable $date
         *
         * @return Session
         */
         *
         * @return Session
         */
-       public function setDate(\DateTime $date): Session {
+       public function setDate(\DateTimeImmutable $date): Session {
                $this->date = $date;
 
                return $this;
                $this->date = $date;
 
                return $this;
@@ -178,20 +153,20 @@ class Session {
        /**
         * Get date
         *
        /**
         * Get date
         *
-        * @return \DateTime
+        * @return \DateTimeImmutable
         */
         */
-       public function getDate(): \DateTime {
+       public function getDate(): \DateTimeImmutable {
                return $this->date;
        }
 
        /**
         * Set begin
         *
                return $this->date;
        }
 
        /**
         * Set begin
         *
-        * @param \DateTime $begin
+        * @param \DateTimeImmutable $begin
         *
         * @return Session
         */
         *
         * @return Session
         */
-       public function setBegin(?\DateTime $begin): Session {
+       public function setBegin(?\DateTimeImmutable $begin): Session {
                $this->begin = $begin;
 
                return $this;
                $this->begin = $begin;
 
                return $this;
@@ -200,18 +175,18 @@ class Session {
        /**
         * Get begin
         *
        /**
         * Get begin
         *
-        * @return \DateTime
+        * @return \DateTimeImmutable
         */
         */
-       public function getBegin(): ?\DateTime {
+       public function getBegin(): ?\DateTimeImmutable {
                return $this->begin;
        }
 
        /**
         * Get start
         *
                return $this->begin;
        }
 
        /**
         * Get start
         *
-        * @return \DateTime
+        * @return \DateTimeImmutable
         */
         */
-       public function getStart(): \DateTime {
+       public function getStart(): \DateTimeImmutable {
                //With start
                if ($this->start !== null) {
                        return $this->start;
                //With start
                if ($this->start !== null) {
                        return $this->start;
@@ -240,11 +215,11 @@ class Session {
        /**
         * Set length
         *
        /**
         * Set length
         *
-        * @param \DateTime $length
+        * @param \DateTimeImmutable $length
         *
         * @return Session
         */
         *
         * @return Session
         */
-       public function setLength(?\DateTime $length): Session {
+       public function setLength(?\DateTimeImmutable $length): Session {
                $this->length = $length;
 
                return $this;
                $this->length = $length;
 
                return $this;
@@ -253,18 +228,18 @@ class Session {
        /**
         * Get length
         *
        /**
         * Get length
         *
-        * @return \DateTime
+        * @return \DateTimeImmutable
         */
         */
-       public function getLength(): ?\DateTime {
+       public function getLength(): ?\DateTimeImmutable {
                return $this->length;
        }
 
        /**
         * Get stop
         *
                return $this->length;
        }
 
        /**
         * Get stop
         *
-        * @return \DateTime
+        * @return \DateTimeImmutable
         */
         */
-       public function getStop(): \DateTime {
+       public function getStop(): \DateTimeImmutable {
                //Check start
                if ($this->stop !== null) {
                        return $this->stop;
                //Check start
                if ($this->stop !== null) {
                        return $this->stop;
@@ -290,7 +265,7 @@ class Session {
         *
         * @return Session
         */
         *
         * @return Session
         */
-       public function setPremium(bool $premium): Session {
+       public function setPremium(?bool $premium): Session {
                $this->premium = $premium;
 
                return $this;
                $this->premium = $premium;
 
                return $this;
@@ -301,7 +276,7 @@ class Session {
         *
         * @return bool
         */
         *
         * @return bool
         */
-       public function getPremium(): bool {
+       public function getPremium(): ?bool {
                return $this->premium;
        }
 
                return $this->premium;
        }
 
@@ -484,11 +459,11 @@ class Session {
        /**
         * Set locked
         *
        /**
         * Set locked
         *
-        * @param \DateTime $locked
+        * @param \DateTimeImmutable $locked
         *
         * @return Session
         */
         *
         * @return Session
         */
-       public function setLocked(?\DateTime $locked): Session {
+       public function setLocked(?\DateTimeImmutable $locked): Session {
                $this->locked = $locked;
 
                return $this;
                $this->locked = $locked;
 
                return $this;
@@ -497,20 +472,20 @@ class Session {
        /**
         * Get locked
         *
        /**
         * Get locked
         *
-        * @return \DateTime
+        * @return \DateTimeImmutable
         */
         */
-       public function getLocked(): ?\DateTime {
+       public function getLocked(): ?\DateTimeImmutable {
                return $this->locked;
        }
 
        /**
         * Set created
         *
                return $this->locked;
        }
 
        /**
         * Set created
         *
-        * @param \DateTime $created
+        * @param \DateTimeImmutable $created
         *
         * @return Session
         */
         *
         * @return Session
         */
-       public function setCreated(\DateTime $created): Session {
+       public function setCreated(\DateTimeImmutable $created): Session {
                $this->created = $created;
 
                return $this;
                $this->created = $created;
 
                return $this;
@@ -519,20 +494,20 @@ class Session {
        /**
         * Get created
         *
        /**
         * Get created
         *
-        * @return \DateTime
+        * @return \DateTimeImmutable
         */
         */
-       public function getCreated(): \DateTime {
+       public function getCreated(): \DateTimeImmutable {
                return $this->created;
        }
 
        /**
         * Set updated
         *
                return $this->created;
        }
 
        /**
         * Set updated
         *
-        * @param \DateTime $updated
+        * @param \DateTimeImmutable $updated
         *
         * @return Session
         */
         *
         * @return Session
         */
-       public function setUpdated(\DateTime $updated): Session {
+       public function setUpdated(\DateTimeImmutable $updated): Session {
                $this->updated = $updated;
 
                return $this;
                $this->updated = $updated;
 
                return $this;
@@ -541,9 +516,9 @@ class Session {
        /**
         * Get updated
         *
        /**
         * Get updated
         *
-        * @return \DateTime
+        * @return \DateTimeImmutable
         */
         */
-       public function getUpdated(): \DateTime {
+       public function getUpdated(): \DateTimeImmutable {
                return $this->updated;
        }
 
                return $this->updated;
        }
 
@@ -649,9 +624,9 @@ class Session {
         */
        public function preUpdate(PreUpdateEventArgs $eventArgs) {
                //Check that we have a session instance
         */
        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
                        //Set updated value
-                       $session->setUpdated(new \DateTime('now'));
+                       $session->setUpdated(new \DateTimeImmutable('now'));
                }
        }
 
                }
        }
 
@@ -661,6 +636,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
         *
         * 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 {
         * @return bool Whether the date is day off or not
         */
        public function isPremium(): bool {
@@ -731,7 +708,7 @@ class Session {
                }
 
                //Get eastern
                }
 
                //Get eastern
-               $eastern = $this->getEastern($date->format('Y'));
+               $eastern = (new IntlUtil())->getEastern($date->format('Y'));
 
                //Check dynamic holidays
                if (
 
                //Check dynamic holidays
                if (
@@ -758,48 +735,4 @@ class Session {
                //Date is not a holiday and week day
                return false;
        }
                //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)));
-       }
 }
 }