1 <?php
declare(strict_types
=1);
4 * this file is part of the rapsys packbundle package.
6 * (c) raphaël gertz <symfony@rapsys.eu>
8 * for the full copyright and license information, please view the license
9 * file that was distributed with this source code.
12 namespace Rapsys\AirBundle\Entity
;
14 use Doctrine\Common\Collections\ArrayCollection
;
15 use Doctrine\ORM\Event\PreUpdateEventArgs
;
89 private $temperaturemin;
94 private $temperaturemax;
114 private $application;
127 * @var ArrayCollection
129 private $applications;
134 public function __construct() {
138 $this->length
= null;
140 $this->premium
= null;
141 $this->rainfall
= null;
142 $this->rainrisk
= null;
143 $this->realfeel
= null;
144 $this->realfeelmin
= null;
145 $this->realfeelmax
= null;
146 $this->temperature
= null;
147 $this->temperaturemin
= null;
148 $this->temperaturemax
= null;
149 $this->locked
= null;
150 $this->created
= new \
DateTime('now');
151 $this->updated
= new \
DateTime('now');
152 $this->applications
= new ArrayCollection();
160 public function getId(): int {
167 * @param \DateTime $date
171 public function setDate(\DateTime
$date): Session
{
182 public function getDate(): \DateTime
{
189 * @param \DateTime $begin
193 public function setBegin(?\DateTime
$begin): Session
{
194 $this->begin
= $begin;
204 public function getBegin(): ?\DateTime
{
213 public function getStart(): \DateTime
{
215 if ($this->start
!== null) {
220 $this->start
= clone $this->date
;
222 //Check if after slot
223 //XXX: id=4 <=> title=After
224 if ($this->slot
->getId() == 4) {
226 $this->start
->add(new \
DateInterval('P1D'));
230 if ($this->begin
!== null) {
232 $this->start
->setTime(intval($this->begin
->format('H')), intval($this->begin
->format('i')), intval($this->begin
->format('s')));
242 * @param \DateTime $length
246 public function setLength(?\DateTime
$length): Session
{
247 $this->length
= $length;
257 public function getLength(): ?\DateTime
{
258 return $this->length
;
266 public function getStop(): \DateTime
{
268 if ($this->stop
!== null) {
273 $this->stop
= clone $this->getStart();
276 if ($this->length
!== null) {
278 $this->stop
->add(new \
DateInterval('PT'.$this->length
->format('H').'H'.$this->length
->format('i').'M'.$this->length
->format('s').'S'));
288 * @param boolean $premium
292 public function setPremium(bool $premium): Session
{
293 $this->premium
= $premium;
303 public function getPremium(): bool {
304 return $this->premium
;
310 * @param float $rainfall
314 public function setRainfall(?float $rainfall): Session
{
315 $this->rainfall
= $rainfall;
325 public function getRainfall(): ?float {
326 return $this->rainfall
;
332 * @param float $rainrisk
336 public function setRainrisk(?float $rainrisk): Session
{
337 $this->rainrisk
= $rainrisk;
347 public function getRainrisk(): ?float {
348 return $this->rainrisk
;
354 * @param float $realfeel
358 public function setRealfeel(?float $realfeel): Session
{
359 $this->realfeel
= $realfeel;
369 public function getRealfeel(): ?float {
370 return $this->realfeel
;
376 * @param float $realfeelmin
380 public function setRealfeelmin(?float $realfeelmin): Session
{
381 $this->realfeelmin
= $realfeelmin;
391 public function getRealfeelmin(): ?float {
392 return $this->realfeelmin
;
398 * @param float $realfeelmax
402 public function setRealfeelmax(?float $realfeelmax): Session
{
403 $this->realfeelmax
= $realfeelmax;
413 public function getRealfeelmax(): ?float {
414 return $this->realfeelmax
;
420 * @param float $temperature
424 public function setTemperature(?float $temperature): Session
{
425 $this->temperature
= $temperature;
435 public function getTemperature(): ?float {
436 return $this->temperature
;
442 * @param float $temperaturemin
446 public function setTemperaturemin(?float $temperaturemin): Session
{
447 $this->temperaturemin
= $temperaturemin;
457 public function getTemperaturemin(): ?float {
458 return $this->temperaturemin
;
464 * @param float $temperaturemax
468 public function setTemperaturemax(?float $temperaturemax): Session
{
469 $this->temperaturemax
= $temperaturemax;
479 public function getTemperaturemax(): ?float {
480 return $this->temperaturemax
;
486 * @param \DateTime $locked
490 public function setLocked(?\DateTime
$locked): Session
{
491 $this->locked
= $locked;
501 public function getLocked(): ?\DateTime
{
502 return $this->locked
;
508 * @param \DateTime $created
512 public function setCreated(\DateTime
$created): Session
{
513 $this->created
= $created;
523 public function getCreated(): \DateTime
{
524 return $this->created
;
530 * @param \DateTime $updated
534 public function setUpdated(\DateTime
$updated): Session
{
535 $this->updated
= $updated;
545 public function getUpdated(): \DateTime
{
546 return $this->updated
;
552 * @param Application $application
556 public function addApplication(Application
$application): Session
{
557 $this->applications
[] = $application;
565 * @param Application $application
567 public function removeApplication(Application
$application): bool {
568 return $this->applications
->removeElement($application);
574 * @return ArrayCollection
576 public function getApplications(): ArrayCollection
{
577 return $this->applications
;
583 * @param Location $location
587 public function setLocation(Location
$location): Session
{
588 $this->location
= $location;
598 public function getLocation(): Location
{
599 return $this->location
;
609 public function setSlot(Slot
$slot): Session
{
620 public function getSlot(): Slot
{
627 * @param Application $application
631 public function setApplication(Application
$application): Session
{
632 $this->application
= $application;
640 * @return Application
642 public function getApplication(): ?Application
{
643 return $this->application
;
649 public function preUpdate(PreUpdateEventArgs
$eventArgs) {
650 //Check that we have a session instance
651 if (($session = $eventArgs->getEntity()) instanceof Session
) {
653 $session->setUpdated(new \
DateTime('now'));
658 * Wether if session is a premium day
660 * Consider as premium a day off for afternoon, the eve for evening and after
661 * Store computed result in premium member for afternoon and evening
663 * @return bool Whether the date is day off or not
665 public function isPremium(): bool {
667 if (empty($date = $this->date
)) {
668 throw new \
LogicException('Property date is empty');
672 if (empty($slot = $this->slot
) || empty($slotTitle = $slot->getTitle())) {
673 throw new \
LogicException('Property slot is empty');
676 //With evening and after slot
677 if ($slotTitle == 'Evening' || $slotTitle == 'After') {
678 //Evening and after session is considered premium when the eve is a day off
679 $date = (clone $date)->add(new \
DateInterval('P1D'));
683 $w = $date->format('w');
685 //Check if weekend day
686 if ($w == 0 || $w == 6) {
687 //With afternoon and evening slot
688 if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
690 $this->premium
= true;
693 //Date is weekend day
698 $d = $date->format('d');
701 $m = $date->format('m');
703 //Check if fixed holiday
705 //Check if 1st january
706 ($d == 1 && $m == 1) ||
708 ($d == 1 && $m == 5) ||
710 ($d == 8 && $m == 5) ||
712 ($d == 14 && $m == 7) ||
713 //Check if 15st august
714 ($d == 15 && $m == 8) ||
715 //Check if 1st november
716 ($d == 1 && $m == 11) ||
717 //Check if 11st november
718 ($d == 11 && $m == 11) ||
719 //Check if 25st december
720 ($d == 25 && $m == 12)
722 //With afternoon and evening slot
723 if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
725 $this->premium
= true;
728 //Date is a fixed holiday
733 $eastern = $this->getEastern($date->format('Y'));
735 //Check dynamic holidays
737 (clone $eastern)->add(new \
DateInterval('P1D')) == $date ||
738 (clone $eastern)->add(new \
DateInterval('P39D')) == $date ||
739 (clone $eastern)->add(new \
DateInterval('P50D')) == $date
741 //With afternoon and evening slot
742 if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
744 $this->premium
= true;
747 //Date is a dynamic holiday
751 //With afternoon and evening slot
752 if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
754 $this->premium
= false;
757 //Date is not a holiday and week day
762 * Compute eastern for selected year
764 * @param string $year The eastern year
766 * @return DateTime The eastern date
768 private function getEastern(string $year): \DateTime
{
772 //Check if already computed
773 if (isset($data[$year])) {
774 //Return computed eastern
776 //Check if data is null
777 } elseif (is_null($data)) {
782 $d = (19 * ($year %
19) +
24) %
30;
784 $e = (2 * ($year %
4) +
4 * ($year %
7) +
6 * $d +
5) %
7;
793 } elseif ($d == 29 && $e == 6) {
796 } elseif ($d == 28 && $e == 6) {
801 //Store eastern in data
802 return ($data[$year] = new \
DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day)));