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\Collection
;
15 use Doctrine\Common\Collections\ArrayCollection
;
16 use Doctrine\ORM\Event\PreUpdateEventArgs
;
18 use Rapsys\PackBundle\Util\IntlUtil
;
27 private ?int $id = null;
32 private ?\DateTime
$begin = null;
35 * Computed start datetime
37 private ?\DateTime
$start = null;
42 private ?\DateTime
$length = null;
45 * Computed stop datetime
47 private ?\DateTime
$stop = null;
52 private ?bool $premium = null;
57 private ?float $rainfall = null;
62 private ?float $rainrisk = null;
65 * Real feel temperature
67 private ?float $realfeel = null;
70 * Real feel minimum temperature
72 private ?float $realfeelmin = null;
75 * Real feel maximum temperature
77 private ?float $realfeelmax = null;
82 private ?float $temperature = null;
87 private ?float $temperaturemin = null;
92 private ?float $temperaturemax = null;
97 private ?\DateTime
$locked = null;
102 private \DateTime
$created;
107 private \DateTime
$updated;
110 * Application instance
112 private ?Application
$application = null;
115 * Applications collection
117 private Collection
$applications;
122 public function __construct(private \DateTime
$date, private Location
$location, private Slot
$slot) {
124 $this->created
= new \
DateTime('now');
125 $this->updated
= new \
DateTime('now');
126 $this->applications
= new ArrayCollection();
134 public function getId(): ?int {
141 * @param \DateTime $date
145 public function setDate(\DateTime
$date): Session
{
156 public function getDate(): \DateTime
{
163 * @param \DateTime $begin
167 public function setBegin(?\DateTime
$begin): Session
{
168 $this->begin
= $begin;
178 public function getBegin(): ?\DateTime
{
187 public function getStart(): \DateTime
{
189 if ($this->start
!== null) {
194 $this->start
= clone $this->date
;
196 //Check if after slot
197 //XXX: id=4 <=> title=After
198 if ($this->slot
->getId() == 4) {
200 $this->start
->add(new \
DateInterval('P1D'));
204 if ($this->begin
!== null) {
206 $this->start
->setTime(intval($this->begin
->format('H')), intval($this->begin
->format('i')), intval($this->begin
->format('s')));
216 * @param \DateTime $length
220 public function setLength(?\DateTime
$length): Session
{
221 $this->length
= $length;
231 public function getLength(): ?\DateTime
{
232 return $this->length
;
240 public function getStop(): \DateTime
{
242 if ($this->stop
!== null) {
247 $this->stop
= clone $this->getStart();
250 if ($this->length
!== null) {
252 $this->stop
->add(new \
DateInterval('PT'.$this->length
->format('H').'H'.$this->length
->format('i').'M'.$this->length
->format('s').'S'));
262 * @param bool $premium
266 public function setPremium(?bool $premium): Session
{
267 $this->premium
= $premium;
277 public function getPremium(): ?bool {
278 return $this->premium
;
284 * @param float $rainfall
288 public function setRainfall(?float $rainfall): Session
{
289 $this->rainfall
= $rainfall;
299 public function getRainfall(): ?float {
300 return $this->rainfall
;
306 * @param float $rainrisk
310 public function setRainrisk(?float $rainrisk): Session
{
311 $this->rainrisk
= $rainrisk;
321 public function getRainrisk(): ?float {
322 return $this->rainrisk
;
328 * @param float $realfeel
332 public function setRealfeel(?float $realfeel): Session
{
333 $this->realfeel
= $realfeel;
343 public function getRealfeel(): ?float {
344 return $this->realfeel
;
350 * @param float $realfeelmin
354 public function setRealfeelmin(?float $realfeelmin): Session
{
355 $this->realfeelmin
= $realfeelmin;
365 public function getRealfeelmin(): ?float {
366 return $this->realfeelmin
;
372 * @param float $realfeelmax
376 public function setRealfeelmax(?float $realfeelmax): Session
{
377 $this->realfeelmax
= $realfeelmax;
387 public function getRealfeelmax(): ?float {
388 return $this->realfeelmax
;
394 * @param float $temperature
398 public function setTemperature(?float $temperature): Session
{
399 $this->temperature
= $temperature;
409 public function getTemperature(): ?float {
410 return $this->temperature
;
416 * @param float $temperaturemin
420 public function setTemperaturemin(?float $temperaturemin): Session
{
421 $this->temperaturemin
= $temperaturemin;
431 public function getTemperaturemin(): ?float {
432 return $this->temperaturemin
;
438 * @param float $temperaturemax
442 public function setTemperaturemax(?float $temperaturemax): Session
{
443 $this->temperaturemax
= $temperaturemax;
453 public function getTemperaturemax(): ?float {
454 return $this->temperaturemax
;
460 * @param \DateTime $locked
464 public function setLocked(?\DateTime
$locked): Session
{
465 $this->locked
= $locked;
475 public function getLocked(): ?\DateTime
{
476 return $this->locked
;
482 * @param \DateTime $created
486 public function setCreated(\DateTime
$created): Session
{
487 $this->created
= $created;
497 public function getCreated(): \DateTime
{
498 return $this->created
;
504 * @param \DateTime $updated
508 public function setUpdated(\DateTime
$updated): Session
{
509 $this->updated
= $updated;
519 public function getUpdated(): \DateTime
{
520 return $this->updated
;
526 * @param Application $application
530 public function addApplication(Application
$application): Session
{
531 $this->applications
[] = $application;
539 * @param Application $application
541 public function removeApplication(Application
$application): bool {
542 return $this->applications
->removeElement($application);
548 * @return ArrayCollection
550 public function getApplications(): ArrayCollection
{
551 return $this->applications
;
557 * @param Location $location
561 public function setLocation(Location
$location): Session
{
562 $this->location
= $location;
572 public function getLocation(): Location
{
573 return $this->location
;
583 public function setSlot(Slot
$slot): Session
{
594 public function getSlot(): Slot
{
601 * @param Application $application
605 public function setApplication(?Application
$application): Session
{
606 $this->application
= $application;
614 * @return Application
616 public function getApplication(): ?Application
{
617 return $this->application
;
623 public function preUpdate(PreUpdateEventArgs
$eventArgs) {
624 //Check that we have a session instance
625 if (($session = $eventArgs->getObject()) instanceof Session
) {
627 $session->setUpdated(new \
DateTime('now'));
632 * Wether if session is a premium day
634 * Consider as premium a day off for afternoon, the eve for evening and after
635 * Store computed result in premium member for afternoon and evening
637 * @TODO improve by moving day off computation in IntlUtil or HolidayUtil class ?
639 * @return bool Whether the date is day off or not
641 public function isPremium(): bool {
643 if (empty($date = $this->date
)) {
644 throw new \
LogicException('Property date is empty');
648 if (empty($slot = $this->slot
) || empty($slotTitle = $slot->getTitle())) {
649 throw new \
LogicException('Property slot is empty');
652 //With evening and after slot
653 if ($slotTitle == 'Evening' || $slotTitle == 'After') {
654 //Evening and after session is considered premium when the eve is a day off
655 $date = (clone $date)->add(new \
DateInterval('P1D'));
659 $w = $date->format('w');
661 //Check if weekend day
662 if ($w == 0 || $w == 6) {
663 //With afternoon and evening slot
664 if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
666 $this->premium
= true;
669 //Date is weekend day
674 $d = $date->format('d');
677 $m = $date->format('m');
679 //Check if fixed holiday
681 //Check if 1st january
682 ($d == 1 && $m == 1) ||
684 ($d == 1 && $m == 5) ||
686 ($d == 8 && $m == 5) ||
688 ($d == 14 && $m == 7) ||
689 //Check if 15st august
690 ($d == 15 && $m == 8) ||
691 //Check if 1st november
692 ($d == 1 && $m == 11) ||
693 //Check if 11st november
694 ($d == 11 && $m == 11) ||
695 //Check if 25st december
696 ($d == 25 && $m == 12)
698 //With afternoon and evening slot
699 if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
701 $this->premium
= true;
704 //Date is a fixed holiday
709 $eastern = (new IntlUtil())->getEastern($date->format('Y'));
711 //Check dynamic holidays
713 (clone $eastern)->add(new \
DateInterval('P1D')) == $date ||
714 (clone $eastern)->add(new \
DateInterval('P39D')) == $date ||
715 (clone $eastern)->add(new \
DateInterval('P50D')) == $date
717 //With afternoon and evening slot
718 if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
720 $this->premium
= true;
723 //Date is a dynamic holiday
727 //With afternoon and evening slot
728 if ($slotTitle == 'Afternoon' || $slotTitle == 'Evening') {
730 $this->premium
= false;
733 //Date is not a holiday and week day