Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.92% covered (success)
96.92%
63 / 65
94.74% covered (success)
94.74%
18 / 19
CRAP
0.00% covered (danger)
0.00%
0 / 1
Appointment
96.92% covered (success)
96.92%
63 / 65
94.74% covered (success)
94.74%
18 / 19
30
0.00% covered (danger)
0.00%
0 / 1
 getDefaults
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 toDate
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 toTime
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 hasTime
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 setTime
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 setDateTime
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addDate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addScope
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getScope
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 addSlotCount
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getSlotCount
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAvailability
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 toDateTime
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getStartTime
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getEndTime
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getEndTimeWithCustomSlotTime
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setDateByString
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 isMatching
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 __toString
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace BO\Zmsentities;
4
5use BO\Zmsentities\Helper\Property;
6
7class Appointment extends Schema\Entity
8{
9    public const PRIMARY = 'id';
10
11    public static $schema = "appointment.json";
12
13    #[\Override]
14    public function getDefaults()
15    {
16        return [
17            'date' => 0,
18            'scope' => new Scope(),
19            'availability' => new Availability(),
20            'slotCount' => 0,
21        ];
22    }
23
24    public function toDate($lang = 'de')
25    {
26        // Mittwoch 18. November 2015
27        return ($lang == 'en') ? date('l F d, Y', $this->date) : Helper\DateTime::getFormatedDates(
28            $this->toDateTime(),
29            'EEEE dd. MMMM yyyy'
30        );
31    }
32
33    public function toTime($lang = 'de')
34    {
35        $suffix = ($lang == 'en') ? ' o\'clock' : ' Uhr';
36        return date('H:i', $this->date) . $suffix;
37    }
38
39    public function hasTime()
40    {
41        if ($this->date == 0) {
42            return false;
43        }
44        $time = $this->toDateTime();
45        return ('00:00' != $time->format('H:i'));
46    }
47
48    /**
49     * Modify time for appointment
50     *
51     */
52    public function setTime($timeString)
53    {
54        $dateTime = $this->toDateTime();
55        $this->date = $dateTime->modify($timeString)->getTimestamp();
56        return $this;
57    }
58
59    public function setDateTime(\DateTimeInterface $dateTime)
60    {
61        $this->date = $dateTime->getTimestamp();
62        return $this;
63    }
64
65    public function addDate($date)
66    {
67        $this->date = $date;
68        return $this;
69    }
70
71    public function addScope($scopeId)
72    {
73        $this->getScope()->id = $scopeId;
74        return $this;
75    }
76
77    public function getScope()
78    {
79        $this->scope = ($this->toProperty()->scope->isAvailable()) ? new Scope($this['scope']) : new Scope();
80        return $this->scope;
81    }
82
83    public function addSlotCount($slotCount = null)
84    {
85        if ($slotCount) {
86            $this->slotCount = $slotCount;
87        } else {
88            $this->slotCount += 1;
89        }
90
91        return $this;
92    }
93
94    public function getSlotCount()
95    {
96        return $this->slotCount;
97    }
98
99    public function getAvailability()
100    {
101        $data = array();
102        if (Property::__keyExists('availability', $this)) {
103            $data = $this['availability'];
104        }
105        return new Availability($data);
106    }
107
108    public function toDateTime($timezone = 'Europe/Berlin')
109    {
110        $date = (new \DateTimeImmutable())->setTimestamp($this->date);
111        //$date = \DateTimeImmutable::createFromFormat("U", $this->date);
112        if ($date) {
113            $date = $date->setTimeZone(new \DateTimeZone($timezone));
114        }
115        return $date;
116    }
117
118    public function getStartTime()
119    {
120        $time = $this->toDateTime();
121        return $time;
122    }
123
124    public function getEndTime()
125    {
126        $time = $this->getStartTime();
127        $availability = $this->getAvailability();
128        return ($availability->slotTimeInMinutes)
129          ? $time->modify('+' . ($availability->slotTimeInMinutes * $this->slotCount) . ' minutes')
130          : $time;
131    }
132
133    public function getEndTimeWithCustomSlotTime($slotTimeInMinutes)
134    {
135        $time = $this->getStartTime();
136        return $time->modify('+' . ($slotTimeInMinutes * $this->slotCount) . ' minutes');
137    }
138
139    public function setDateByString($dateString, $format = 'Y-m-d H:i')
140    {
141        $appointmentDateTime = \DateTimeImmutable::createFromFormat($format, $dateString);
142        if ($appointmentDateTime) {
143            $this->date = $appointmentDateTime->format('U');
144        } else {
145            throw new Exception\DateStringWrongFormat(
146                "String " . htmlspecialchars($dateString) . " not format " . htmlspecialchars($format)
147            );
148        }
149        return $this;
150    }
151
152    /**
153     * Does two appointments match, the matching appointment might have a lower slot count
154     *
155     */
156    public function isMatching(self $appointment)
157    {
158        if (
159            $appointment['scope']['id'] == $this['scope']['id']
160            && $appointment['date'] == $this['date']
161        ) {
162            return true;
163        }
164        return false;
165    }
166
167    public function __toString()
168    {
169        return "appointment#"
170            . $this->toDateTime()->format('c')
171            . " " . $this['slotCount'] . "slots"
172            . " scope" . $this['scope']['id']
173            ;
174    }
175}