Lines 91.89% 136 / 148
Functions and Methods 76.92% 10 / 13
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Availability 91.89% 136 / 148 76.92% 10 / 13 42.94 0.00% 0 / 1
 readEntity 100.00% 11 / 11 100.00% 1 / 1 3
 readResolvedReferences 100.00% 8 / 8 100.00% 1 / 1 4
 readLock 100.00% 1 / 1 100.00% 1 / 1 1
 readEntityDoubleTypes 100.00% 9 / 9 100.00% 1 / 1 2
 readList 96.29% 26 / 27 0.00% 0 / 1 9
 readAvailabilityListByScope 100.00% 18 / 18 100.00% 1 / 1 7
 readAvailabilityListBefore 100.00% 13 / 13 100.00% 1 / 1 4
 readOpeningHoursListByDate 100.00% 16 / 16 100.00% 1 / 1 4
 readByAppointment 0.00% 0 / 10 0.00% 0 / 1 2
 readListByAppointment 100.00% 14 / 14 100.00% 1 / 1 3
 writeEntity 88.88% 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
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(string|int $availabilityId)
57    {
58        return $this->perform(\BO\Zmsbackend\Availability\Repository\Availability::QUERY_GET_LOCK, ['availabilityId' => $availabilityId]);
59    }
60
61    public function readEntityDoubleTypes(string|int $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        string|int $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    /*
147    ** Terminkunden and Spontankunden rows whose end date is on or before $datetime
148    */
149    public function readAvailabilityListBefore(\DateTimeImmutable $datetime, int $resolveReferences = 0): Collection
150    {
151        $collection = new Collection();
152        $query = new \BO\Zmsbackend\Availability\Repository\Availability(\BO\Zmsbackend\Query\Base::SELECT);
153        $query
154            ->addEntityMapping()
155            ->addResolvedReferences($resolveReferences)
156            ->addConditionOnlyOld($datetime);
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    public function readOpeningHoursListByDate(string|int $scopeId, \DateTimeInterface $now, int $resolveReferences = 0): Collection
170    {
171        $collection = new Collection();
172        $query = new \BO\Zmsbackend\Availability\Repository\Availability(\BO\Zmsbackend\Query\Base::SELECT);
173        $query
174            ->addEntityMapping('openinghours')
175            ->addResolvedReferences($resolveReferences)
176            ->addConditionScopeId($scopeId)
177            ->addConditionOpeningHours()
178            ->addConditionDate($now);
179        $result = $this->fetchList($query, new Entity());
180        if (count($result)) {
181            foreach ($result as $entity) {
182                if ($entity instanceof Entity) {
183                    $entity = $this->readResolvedReferences($entity, $resolveReferences);
184                    $entity->type = 'openinghours';
185                    $collection->addEntity($entity);
186                }
187            }
188        }
189        return $collection;
190    }
191
192    public function readByAppointment(\BO\Zmsentities\Appointment $appointment, int $resolveReferences = 0)
193    {
194        $query = new \BO\Zmsbackend\Availability\Repository\Availability(\BO\Zmsbackend\Query\Base::SELECT);
195        $query->addEntityMapping();
196        $query->addResolvedReferences($resolveReferences);
197        $query->addConditionScopeId($appointment->toProperty()->scope->id->get());
198        $query->addConditionDate($appointment->toDateTime());
199        $query->addConditionAppointmentTime($appointment->toDateTime());
200        $entity = $this->fetchOne($query, new Entity());
201        $entity->scope = $appointment->scope;
202        $entity = $this->readResolvedReferences($entity, $resolveReferences);
203        return $entity;
204    }
205
206    /**
207     * Read all availabilities matching an appointment, overlapping availabilities return more than one entry
208     */
209    public function readListByAppointment(
210        \BO\Zmsentities\Appointment $appointment,
211        int $resolveReferences = 0
212    ): Collection {
213        $collection = new Collection();
214        $query = new \BO\Zmsbackend\Availability\Repository\Availability(\BO\Zmsbackend\Query\Base::SELECT);
215        $query->addEntityMapping();
216        $query->addResolvedReferences($resolveReferences);
217        $query->addConditionScopeId($appointment->toProperty()->scope->id->get());
218        $query->addConditionDate($appointment->toDateTime());
219        $query->addConditionAppointmentTime($appointment->toDateTime());
220        $result = $this->fetchList($query, new Entity());
221        foreach ($result as $entity) {
222            if ($entity instanceof Entity) {
223                $entity->scope = $appointment->scope;
224                $entity = $this->readResolvedReferences($entity, $resolveReferences);
225                $collection->addEntity($entity);
226            }
227        }
228        return $collection;
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}