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, bool $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        int $resolveReferences,
42        bool $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, int $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, int $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, int $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 \BO\Zmsentities\Availability $entity
235     * @param int $resolveReferences
236     *
237     * @return Entity
238     */
239    public function writeEntity(\BO\Zmsentities\Availability $entity, int $resolveReferences = 0)
240    {
241        self::$cache = [];
242        $entity->testValid();
243        $query = new \BO\Zmsbackend\Availability\Repository\Availability(\BO\Zmsbackend\Query\Base::INSERT);
244        $values = $query->reverseEntityMapping($entity);
245        $query->addValues($values);
246        if (!$this->writeItem($query)) {
247            throw new \BO\Zmsbackend\Availability\Exception\AvailabilityCreateFailed();
248        }
249        $entity->id = $this->getWriter()->lastInsertId();
250        return $this->readEntity($entity->id, $resolveReferences);
251    }
252
253    /**
254     * update an availability
255     *
256     * @param int|string $entityId
257     *
258     * @return Entity
259     *
260     */
261    public function updateEntity($entityId, \BO\Zmsentities\Availability $entity, int $resolveReferences = 0)
262    {
263        self::$cache = [];
264        $entity->testValid();
265        $query = new \BO\Zmsbackend\Availability\Repository\Availability(\BO\Zmsbackend\Query\Base::UPDATE);
266        $query->addConditionAvailabilityId($entityId);
267        $values = $query->reverseEntityMapping($entity);
268        $query->addValues($values);
269        $this->writeItem($query);
270        return $this->readEntity($entityId, $resolveReferences);
271    }
272
273    /**
274     * remove an availability
275     *
276     * @param int|string $availabilityId
277     *
278     * @return Entity|null
279     */
280    public function deleteEntity($availabilityId)
281    {
282        self::$cache = [];
283        $query =  new \BO\Zmsbackend\Availability\Repository\Availability(\BO\Zmsbackend\Query\Base::DELETE);
284        $query->addConditionAvailabilityId($availabilityId);
285        return $this->deleteItem($query);
286    }
287}