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