Lines 90.00% 18 / 20
Functions and Methods 88.88% 8 / 9
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
AvailabilitySnapShot 90.00% 18 / 20 88.88% 8 / 9 11.12 0.00% 0 / 1
 __construct 66.66% 4 / 6 0.00% 0 / 1 3.33
 hasOutdatedAvailability 100.00% 1 / 1 100.00% 1 / 1 1
 hasOutdatedScope 100.00% 1 / 1 100.00% 1 / 1 1
 hasOutdatedDayoff 100.00% 1 / 1 100.00% 1 / 1 1
 hasBookableDateTime 100.00% 1 / 1 100.00% 1 / 1 1
 getLastBookableDateTime 100.00% 1 / 1 100.00% 1 / 1 1
 isOpenedOnLastBookableDay 100.00% 1 / 1 100.00% 1 / 1 1
 isTimeOpenedOnLastBookableDay 100.00% 3 / 3 100.00% 1 / 1 1
 hasBookableDateTimeAfter 100.00% 5 / 5 100.00% 1 / 1 1
1<?php
2
3namespace BO\Zmsbackend\Helper;
4
5class AvailabilitySnapShot
6{
7    public \BO\Zmsentities\Availability $availability;
8    public \DateTimeInterface $dateTime;
9
10    public function __construct(
11        \BO\Zmsentities\Availability $availability,
12        \DateTimeInterface $dateTime
13    ) {
14        if (! $availability->scope instanceof \BO\Zmsentities\Scope) {
15            throw new \Exception("Invalid Scope for AvailabilitySnapShot");
16        }
17        if (! $availability->scope->dayoff instanceof \BO\Zmsentities\Collection\DayoffList) {
18            throw new \Exception("Invalid DayoffList for AvailabilitySnapShot");
19        }
20        $this->availability = clone $availability;
21        $this->dateTime = $dateTime;
22    }
23
24    public function hasOutdatedAvailability(): bool
25    {
26        // Lower compared date by one second to make a "<=" comparision
27        return $this->availability->isNewerThan($this->dateTime->modify("-1 second"));
28    }
29
30    public function hasOutdatedScope()
31    {
32        return $this->availability->scope->isNewerThan($this->dateTime);
33    }
34
35    public function hasOutdatedDayoff()
36    {
37        // It is sufficient to check, if current availability with dateTime is affected
38        // if a proposedChange could be affected, new slots have to be calculated, so checking already calculated slots
39        // is sufficient
40        return $this->availability->scope->dayoff->isNewerThan($this->dateTime, $this->availability, $this->dateTime);
41    }
42
43    public function hasBookableDateTime(\DateTimeInterface $proposedDateTime): bool
44    {
45        return $this->availability->hasDate($proposedDateTime, $this->dateTime);
46    }
47
48    public function getLastBookableDateTime(): \DateTimeInterface
49    {
50        return $this->availability->getBookableEnd($this->dateTime);
51    }
52
53    public function isOpenedOnLastBookableDay(): bool
54    {
55        return $this->availability->isOpenedOnDate($this->availability->getBookableEnd($this->dateTime));
56    }
57
58    public function isTimeOpenedOnLastBookableDay(): bool
59    {
60        return $this->availability->isOpened(
61            $this->availability->getBookableEnd($this->dateTime)->modify($this->dateTime->format('H:i:s'))
62        );
63    }
64
65    public function hasBookableDateTimeAfter(\DateTimeInterface $proposedDateTime): bool
66    {
67        return $this->availability->hasDateBetween(
68            $proposedDateTime,
69            $this->availability->getBookableEnd($this->dateTime),
70            $this->dateTime
71        );
72    }
73}