Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.11% covered (success)
91.11%
123 / 135
75.00% covered (warning)
75.00%
9 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
Availability
91.11% covered (success)
91.11%
123 / 135
75.00% covered (warning)
75.00%
9 / 12
40.07
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.59% covered (success)
92.59%
25 / 27
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['public'] = 0;
94                if ($entity['type'] == 'appointment') {
95                    $entity['description'] = '';
96                    $entity->id = '__spontan__' . $entity->id;
97                    $entity['type'] = 'openinghours';
98                }
99                $entity['type'] = ($entity['type'] != 'appointment') ? 'openinghours' : $entity['type'];
100                $collection->addEntity($entity);
101            }
102        }
103        return $collection;
104    }
105
106    public function readAvailabilityListByScope(
107        \BO\Zmsentities\Scope $scope,
108        $resolveReferences = 0,
109        \DateTimeImmutable $startDate = null,
110        \DateTimeImmutable $endDate = null
111    ) {
112        $collection = new Collection();
113        $query = new Query\Availability(Query\Base::SELECT);
114        $query
115            ->addEntityMapping()
116            ->addResolvedReferences($resolveReferences)
117            ->addConditionAppointmentHours()
118            ->addConditionScopeId($scope->id);
119        if ($startDate && $endDate) {
120            $query->addConditionTimeframe($startDate, $endDate);
121        } elseif ($startDate) {
122            $query->addConditionSkipOld($startDate);
123        }
124
125        $result = $this->fetchList($query, new Entity());
126        if (count($result)) {
127            foreach ($result as $entity) {
128                if ($entity instanceof Entity) {
129                    $entity['scope'] = clone $scope;
130                    //skip resolveReferences for using the scope given, TODO check resolvedLevel from scope
131                    $collection->addEntity($entity);
132                }
133            }
134        }
135        return $collection;
136    }
137
138    /* not in use
139    public function readAvailabilityListByDate($scopeId, \DateTimeInterface $now, $resolveReferences = 0)
140    {
141        $collection = new Collection();
142        $query = new Query\Availability(Query\Base::SELECT);
143        $query
144            ->addEntityMapping()
145            ->addResolvedReferences($resolveReferences)
146            ->addConditionScopeId($scopeId)
147            ->addConditionAppointmentHours()
148            ->addConditionDate($now);
149        $result = $this->fetchList($query, new Entity());
150        if (count($result)) {
151            foreach ($result as $entity) {
152                if ($entity instanceof Entity) {
153                    $entity = $this->readResolvedReferences($entity, $resolveReferences);
154                    $collection->addEntity($entity);
155                }
156            }
157        }
158        return $collection;
159    }
160    */
161
162    /*
163    ** Returns a list of availabilities with end date older than 4 weeks
164    */
165    public function readAvailabilityListBefore(\DateTimeImmutable $datetime, $resolveReferences = 0)
166    {
167        $collection = new Collection();
168        $query = new Query\Availability(Query\Base::SELECT);
169        $query
170            ->addEntityMapping()
171            ->addResolvedReferences($resolveReferences)
172            ->addConditionAppointmentHours()
173            ->addConditionOnlyOld($datetime);
174        $result = $this->fetchList($query, new Entity());
175        if (count($result)) {
176            foreach ($result as $entity) {
177                if ($entity instanceof Entity) {
178                    $entity = $this->readResolvedReferences($entity, $resolveReferences);
179                    $collection->addEntity($entity);
180                }
181            }
182        }
183        return $collection;
184    }
185
186    public function readOpeningHoursListByDate($scopeId, \DateTimeInterface $now, $resolveReferences = 0)
187    {
188        $collection = new Collection();
189        $query = new Query\Availability(Query\Base::SELECT);
190        $query
191            ->addEntityMapping('openinghours')
192            ->addResolvedReferences($resolveReferences)
193            ->addConditionScopeId($scopeId)
194            ->addConditionOpeningHours()
195            ->addConditionDate($now);
196        $result = $this->fetchList($query, new Entity());
197        if (count($result)) {
198            foreach ($result as $entity) {
199                if ($entity instanceof Entity) {
200                    $entity = $this->readResolvedReferences($entity, $resolveReferences);
201                    $entity->type = 'openinghours';
202                    $collection->addEntity($entity);
203                }
204            }
205        }
206        return $collection;
207    }
208
209    public function readByAppointment(\BO\Zmsentities\Appointment $appointment, $resolveReferences = 0)
210    {
211        $query = new Query\Availability(Query\Base::SELECT);
212        $query->addEntityMapping();
213        $query->addResolvedReferences($resolveReferences);
214        $query->addConditionScopeId($appointment->toProperty()->scope->id->get());
215        $query->addConditionDate($appointment->toDateTime());
216        $query->addConditionAppointmentTime($appointment->toDateTime());
217        $entity = $this->fetchOne($query, new Entity());
218        $entity->scope = $appointment->scope;
219        $entity = $this->readResolvedReferences($entity, $resolveReferences);
220        return $entity;
221    }
222
223    /**
224     * write an availability
225     *
226     * @param
227     * entityId
228     *
229     * @return Entity
230     */
231    public function writeEntity(\BO\Zmsentities\Availability $entity, $resolveReferences = 0)
232    {
233        self::$cache = [];
234        $entity->testValid();
235        $query = new Query\Availability(Query\Base::INSERT);
236        $values = $query->reverseEntityMapping($entity);
237        $query->addValues($values);
238        if (!$this->writeItem($query)) {
239            throw new Exception\Availability\AvailabilityCreateFailed();
240        }
241        $entity->id = $this->getWriter()->lastInsertId();
242        return $this->readEntity($entity->id, $resolveReferences);
243    }
244
245    /**
246     * update an availability
247     *
248     * @param
249     * entityId
250     *
251     * @return Entity
252     */
253    public function updateEntity($entityId, \BO\Zmsentities\Availability $entity, $resolveReferences = 0)
254    {
255        self::$cache = [];
256        $entity->testValid();
257        $query = new Query\Availability(Query\Base::UPDATE);
258        $query->addConditionAvailabilityId($entityId);
259        $values = $query->reverseEntityMapping($entity);
260        $query->addValues($values);
261        $this->writeItem($query);
262        return $this->readEntity($entityId, $resolveReferences);
263    }
264
265    /**
266     * remove an availability
267     *
268     * @param
269     * availabilityId
270     *
271     * @return Resource Status
272     */
273    public function deleteEntity($availabilityId)
274    {
275        self::$cache = [];
276        $query =  new Query\Availability(Query\Base::DELETE);
277        $query->addConditionAvailabilityId($availabilityId);
278        return $this->deleteItem($query);
279    }
280}