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