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