Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
90.91% |
120 / 132 |
|
75.00% |
9 / 12 |
CRAP | |
0.00% |
0 / 1 |
Availability | |
90.91% |
120 / 132 |
|
75.00% |
9 / 12 |
40.14 | |
0.00% |
0 / 1 |
readEntity | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
readResolvedReferences | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
readLock | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
readEntityDoubleTypes | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
readList | |
92.86% |
26 / 28 |
|
0.00% |
0 / 1 |
9.03 | |||
readAvailabilityListByScope | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
7 | |||
readAvailabilityListBefore | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
4 | |||
readOpeningHoursListByDate | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
4 | |||
readByAppointment | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
writeEntity | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
2.01 | |||
updateEntity | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
deleteEntity | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdb; |
4 | |
5 | use BO\Zmsentities\Availability as Entity; |
6 | use BO\Zmsentities\Collection\AvailabilityList as Collection; |
7 | |
8 | /** |
9 | * @SuppressWarnings(Public) |
10 | */ |
11 | class Availability extends Base implements Interfaces\ResolveReferences |
12 | { |
13 | public static $cache = []; |
14 | |
15 | public function readEntity($availabilityId, $resolveReferences = 0, $preferCache = false) |
16 | { |
17 | $cacheKey = "$availabilityId-$resolveReferences"; |
18 | if (!$preferCache || !array_key_exists($cacheKey, self::$cache)) { |
19 | $query = new Query\Availability(Query\Base::SELECT); |
20 | $query |
21 | ->addEntityMapping() |
22 | ->addResolvedReferences($resolveReferences) |
23 | ->addConditionAvailabilityId($availabilityId); |
24 | $availability = $this->fetchOne($query, new Entity()); |
25 | $availability = $this->readResolvedReferences($availability, $resolveReferences); |
26 | self::$cache[$cacheKey] = $availability; |
27 | } |
28 | return clone self::$cache[$cacheKey]; |
29 | } |
30 | |
31 | public function readResolvedReferences(\BO\Zmsentities\Schema\Entity $entity, $resolveReferences) |
32 | { |
33 | if (1 <= $resolveReferences && $entity->hasId()) { |
34 | if (isset($entity->scope['id'])) { |
35 | $entity['scope'] = (new Scope())->readEntity($entity->scope['id'], $resolveReferences - 1); |
36 | } |
37 | } |
38 | return $entity; |
39 | } |
40 | |
41 | public function readLock($availabilityId) |
42 | { |
43 | return $this->perform(Query\Availability::QUERY_GET_LOCK, ['availabilityId' => $availabilityId]); |
44 | } |
45 | |
46 | public function readEntityDoubleTypes($availabilityId, $resolveReferences = 0) |
47 | { |
48 | $query = new Query\Availability(Query\Base::SELECT); |
49 | $query |
50 | ->addEntityMapping('openinghours') |
51 | ->addResolvedReferences($resolveReferences) |
52 | ->addConditionAvailabilityId($availabilityId) |
53 | ->addConditionDoubleTypes(); |
54 | $availability = $this->fetchOne($query, new Entity()); |
55 | $availability = $this->readResolvedReferences($availability, $resolveReferences); |
56 | return ($availability->hasId()) ? $availability : null; |
57 | } |
58 | |
59 | public function readList( |
60 | $scopeId, |
61 | $resolveReferences = 0, |
62 | \DateTimeInterface $startDate = null, |
63 | \DateTimeInterface $endDate = null |
64 | ) { |
65 | $scope = new \BO\Zmsentities\Scope(['id' => $scopeId]); |
66 | if (1 <= $resolveReferences) { |
67 | $scope = (new Scope())->readEntity($scopeId, $resolveReferences - 1); |
68 | } |
69 | $collection = $this->readAvailabilityListByScope($scope, $resolveReferences, $startDate, $endDate); |
70 | $query = new Query\Availability(Query\Base::SELECT); |
71 | $query |
72 | ->addEntityMapping('openinghours') |
73 | ->addConditionOpeningHours() |
74 | ->addResolvedReferences($resolveReferences) |
75 | ->addConditionScopeId($scopeId); |
76 | if ($startDate && $endDate) { |
77 | $query->addConditionTimeframe($startDate, $endDate); |
78 | } elseif ($startDate) { |
79 | $query->addConditionSkipOld($startDate); |
80 | } |
81 | $result = $this->fetchList($query, new Entity()); |
82 | if (count($result)) { |
83 | foreach ($result as $entity) { |
84 | $entity['scope'] = clone $scope; |
85 | $entity->workstationCount['intern'] = 0; |
86 | $entity->workstationCount['callcenter'] = 0; |
87 | $entity->workstationCount['public'] = 0; |
88 | if ($entity['type'] == 'appointment') { |
89 | $entity['description'] = ''; |
90 | $entity->id = '__spontan__' . $entity->id; |
91 | $entity['type'] = 'openinghours'; |
92 | } |
93 | $entity['type'] = ($entity['type'] != 'appointment') ? 'openinghours' : $entity['type']; |
94 | $collection->addEntity($entity); |
95 | } |
96 | } |
97 | return $collection; |
98 | } |
99 | |
100 | public function readAvailabilityListByScope( |
101 | \BO\Zmsentities\Scope $scope, |
102 | $resolveReferences = 0, |
103 | \DateTimeImmutable $startDate = null, |
104 | \DateTimeImmutable $endDate = null |
105 | ) { |
106 | $collection = new Collection(); |
107 | $query = new Query\Availability(Query\Base::SELECT); |
108 | $query |
109 | ->addEntityMapping() |
110 | ->addResolvedReferences($resolveReferences) |
111 | ->addConditionAppointmentHours() |
112 | ->addConditionScopeId($scope->id); |
113 | if ($startDate && $endDate) { |
114 | $query->addConditionTimeframe($startDate, $endDate); |
115 | } elseif ($startDate) { |
116 | $query->addConditionSkipOld($startDate); |
117 | } |
118 | |
119 | $result = $this->fetchList($query, new Entity()); |
120 | if (count($result)) { |
121 | foreach ($result as $entity) { |
122 | if ($entity instanceof Entity) { |
123 | $entity['scope'] = clone $scope; |
124 | //skip resolveReferences for using the scope given, TODO check resolvedLevel from scope |
125 | $collection->addEntity($entity); |
126 | } |
127 | } |
128 | } |
129 | return $collection; |
130 | } |
131 | |
132 | /* not in use |
133 | public function readAvailabilityListByDate($scopeId, \DateTimeInterface $now, $resolveReferences = 0) |
134 | { |
135 | $collection = new Collection(); |
136 | $query = new Query\Availability(Query\Base::SELECT); |
137 | $query |
138 | ->addEntityMapping() |
139 | ->addResolvedReferences($resolveReferences) |
140 | ->addConditionScopeId($scopeId) |
141 | ->addConditionAppointmentHours() |
142 | ->addConditionDate($now); |
143 | $result = $this->fetchList($query, new Entity()); |
144 | if (count($result)) { |
145 | foreach ($result as $entity) { |
146 | if ($entity instanceof Entity) { |
147 | $entity = $this->readResolvedReferences($entity, $resolveReferences); |
148 | $collection->addEntity($entity); |
149 | } |
150 | } |
151 | } |
152 | return $collection; |
153 | } |
154 | */ |
155 | |
156 | /* |
157 | ** Returns a list of availabilities with end date older than 4 weeks |
158 | */ |
159 | public function readAvailabilityListBefore(\DateTimeImmutable $datetime, $resolveReferences = 0) |
160 | { |
161 | $collection = new Collection(); |
162 | $query = new Query\Availability(Query\Base::SELECT); |
163 | $query |
164 | ->addEntityMapping() |
165 | ->addResolvedReferences($resolveReferences) |
166 | ->addConditionAppointmentHours() |
167 | ->addConditionOnlyOld($datetime); |
168 | $result = $this->fetchList($query, new Entity()); |
169 | if (count($result)) { |
170 | foreach ($result as $entity) { |
171 | if ($entity instanceof Entity) { |
172 | $entity = $this->readResolvedReferences($entity, $resolveReferences); |
173 | $collection->addEntity($entity); |
174 | } |
175 | } |
176 | } |
177 | return $collection; |
178 | } |
179 | |
180 | public function readOpeningHoursListByDate($scopeId, \DateTimeInterface $now, $resolveReferences = 0) |
181 | { |
182 | $collection = new Collection(); |
183 | $query = new Query\Availability(Query\Base::SELECT); |
184 | $query |
185 | ->addEntityMapping('openinghours') |
186 | ->addResolvedReferences($resolveReferences) |
187 | ->addConditionScopeId($scopeId) |
188 | ->addConditionOpeningHours() |
189 | ->addConditionDate($now); |
190 | $result = $this->fetchList($query, new Entity()); |
191 | if (count($result)) { |
192 | foreach ($result as $entity) { |
193 | if ($entity instanceof Entity) { |
194 | $entity = $this->readResolvedReferences($entity, $resolveReferences); |
195 | $entity->type = 'openinghours'; |
196 | $collection->addEntity($entity); |
197 | } |
198 | } |
199 | } |
200 | return $collection; |
201 | } |
202 | |
203 | public function readByAppointment(\BO\Zmsentities\Appointment $appointment, $resolveReferences = 0) |
204 | { |
205 | $query = new Query\Availability(Query\Base::SELECT); |
206 | $query->addEntityMapping(); |
207 | $query->addResolvedReferences($resolveReferences); |
208 | $query->addConditionScopeId($appointment->toProperty()->scope->id->get()); |
209 | $query->addConditionDate($appointment->toDateTime()); |
210 | $query->addConditionAppointmentTime($appointment->toDateTime()); |
211 | $entity = $this->fetchOne($query, new Entity()); |
212 | $entity->scope = $appointment->scope; |
213 | $entity = $this->readResolvedReferences($entity, $resolveReferences); |
214 | return $entity; |
215 | } |
216 | |
217 | /** |
218 | * write an availability |
219 | * |
220 | * @param |
221 | * entityId |
222 | * |
223 | * @return Entity |
224 | */ |
225 | public function writeEntity(\BO\Zmsentities\Availability $entity, $resolveReferences = 0) |
226 | { |
227 | self::$cache = []; |
228 | $entity->testValid(); |
229 | $query = new Query\Availability(Query\Base::INSERT); |
230 | $values = $query->reverseEntityMapping($entity); |
231 | $query->addValues($values); |
232 | if (!$this->writeItem($query)) { |
233 | throw new Exception\Availability\AvailabilityCreateFailed(); |
234 | } |
235 | $entity->id = $this->getWriter()->lastInsertId(); |
236 | return $this->readEntity($entity->id, $resolveReferences); |
237 | } |
238 | |
239 | /** |
240 | * update an availability |
241 | * |
242 | * @param |
243 | * entityId |
244 | * |
245 | * @return Entity |
246 | */ |
247 | public function updateEntity($entityId, \BO\Zmsentities\Availability $entity, $resolveReferences = 0) |
248 | { |
249 | self::$cache = []; |
250 | $entity->testValid(); |
251 | $query = new Query\Availability(Query\Base::UPDATE); |
252 | $query->addConditionAvailabilityId($entityId); |
253 | $values = $query->reverseEntityMapping($entity); |
254 | $query->addValues($values); |
255 | $this->writeItem($query); |
256 | return $this->readEntity($entityId, $resolveReferences); |
257 | } |
258 | |
259 | /** |
260 | * remove an availability |
261 | * |
262 | * @param |
263 | * availabilityId |
264 | * |
265 | * @return Resource Status |
266 | */ |
267 | public function deleteEntity($availabilityId) |
268 | { |
269 | self::$cache = []; |
270 | $query = new Query\Availability(Query\Base::DELETE); |
271 | $query->addConditionAvailabilityId($availabilityId); |
272 | return $this->deleteItem($query); |
273 | } |
274 | } |