]> Raphaël G. Git Repositories - airbundle/blobdiff - Entity/Session.php
Shorten air bundle route alias
[airbundle] / Entity / Session.php
index f5f0ef555def1e9607c9de464e99e9fbf7827961..f74963cdb338dd2914d7c9fc189e2733abb12761 100644 (file)
 <?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;
 
+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
-        */
-       private $begin;
-
-       /**
-        * @var \DateTime
-        */
-       private $start;
-
-       /**
-        * @var \DateTime
+        * Primary key
         */
-       private $length;
+       private ?int $id = null;
 
        /**
-        * @var \DateTime
+        * Begin time
         */
-       private $stop;
+       private ?\DateTime $begin = null;
 
        /**
-        * @var boolean
+        * Computed start datetime
         */
-       private $premium;
+       private ?\DateTime $start = null;
 
        /**
-        * @var float
+        * Length time
         */
-       private $rainfall;
+       private ?\DateTime $length = null;
 
        /**
-        * @var float
+        * Computed stop datetime
         */
-       private $rainrisk;
+       private ?\DateTime $stop = null;
 
        /**
-        * @var float
+        * Premium
         */
-       private $realfeel;
+       private ?bool $premium = null;
 
        /**
-        * @var float
+        * Rain mm
         */
-       private $realfeelmin;
+       private ?float $rainfall = null;
 
        /**
-        * @var float
+        * Rain chance
         */
-       private $realfeelmax;
+       private ?float $rainrisk = null;
 
        /**
-        * @var float
+        * Real feel temperature
         */
-       private $temperature;
+       private ?float $realfeel = null;
 
        /**
-        * @var float
+        * Real feel minimum temperature
         */
-       private $temperaturemin;
+       private ?float $realfeelmin = null;
 
        /**
-        * @var float
+        * Real feel maximum temperature
         */
-       private $temperaturemax;
+       private ?float $realfeelmax = null;
 
        /**
-        * @var \DateTime
+        * Temperature
         */
-       private $locked;
+       private ?float $temperature = null;
 
        /**
-        * @var \DateTime
+        * Minimum temperature
         */
-       private $created;
+       private ?float $temperaturemin = null;
 
        /**
-        * @var \DateTime
+        * Maximum temperature
         */
-       private $updated;
+       private ?float $temperaturemax = null;
 
        /**
-        * @var Application
+        * Lock datetime
         */
-       private $application;
+       private ?\DateTime $locked = null;
 
        /**
-        * @var Dance
+        * Create datetime
         */
-       private $dance;
+       private \DateTime $created;
 
        /**
-        * @var Location
+        * Update datetime
         */
-       private $location;
+       private \DateTime $updated;
 
        /**
-        * @var Slot
+        * Application instance
         */
-       private $slot;
+       private ?Application $application = null;
 
        /**
-        * @var ArrayCollection
+        * Applications collection
         */
-       private $applications;
+       private Collection $applications;
 
        /**
         * Constructor
         */
-       public function __construct() {
-               $this->begin = null;
-               $this->start = null;
-               $this->length = null;
-               $this->stop = 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->premium = null;
+       public function __construct(private \DateTime $date, private Location $location, private Slot $slot) {
+               //Set defaults
+               $this->created = new \DateTime('now');
+               $this->updated = new \DateTime('now');
+
+               //Set collections
                $this->applications = new ArrayCollection();
        }
 
@@ -159,7 +133,7 @@ class Session {
         *
         * @return integer
         */
-       public function getId(): int {
+       public function getId(): ?int {
                return $this->id;
        }
 
@@ -287,11 +261,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;
@@ -302,7 +276,7 @@ class Session {
         *
         * @return bool
         */
-       public function getPremium(): bool {
+       public function getPremium(): ?bool {
                return $this->premium;
        }
 
@@ -579,28 +553,6 @@ class Session {
                return $this->applications;
        }
 
-       /**
-        * Set dance
-        *
-        * @param Dance $dance
-        *
-        * @return Session
-        */
-       public function setDance(Dance $dance): Session {
-               $this->dance = $dance;
-
-               return $this;
-       }
-
-       /**
-        * Get dance
-        *
-        * @return Dance
-        */
-       public function getDance(): Dance {
-               return $this->dance;
-       }
-
        /**
         * Set location
         *
@@ -652,7 +604,7 @@ class Session {
         *
         * @return Session
         */
-       public function setApplication(Application $application): Session {
+       public function setApplication(?Application $application): Session {
                $this->application = $application;
 
                return $this;
@@ -672,9 +624,9 @@ class Session {
         */
        public function preUpdate(PreUpdateEventArgs $eventArgs) {
                //Check that we have a session instance
-               if (($user = $eventArgs->getEntity()) instanceof Session) {
+               if (($session = $eventArgs->getObject()) instanceof Session) {
                        //Set updated value
-                       $user->setUpdated(new \DateTime('now'));
+                       $session->setUpdated(new \DateTime('now'));
                }
        }
 
@@ -684,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
         *
+        * @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 {
@@ -754,7 +708,7 @@ class Session {
                }
 
                //Get eastern
-               $eastern = $this->getEastern($date->format('Y'));
+               $eastern = (new IntlUtil())->getEastern($date->format('Y'));
 
                //Check dynamic holidays
                if (
@@ -781,48 +735,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)));
-       }
 }