Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.47% covered (success)
96.47%
164 / 170
70.59% covered (warning)
70.59%
12 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
ZmsApiClientService
96.47% covered (success)
96.47%
164 / 170
70.59% covered (warning)
70.59%
12 / 17
57
0.00% covered (danger)
0.00%
0 / 1
 fetchSourceData
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
5
 getOffices
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
 getScopes
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
 getServices
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
 getRequestRelationList
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
 getFreeDays
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
3
 getFreeTimeslots
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
3
 reserveTimeslot
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
6
 submitClientData
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 preconfirmProcess
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 confirmProcess
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 cancelAppointment
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 sendConfirmationEmail
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 sendPreconfirmationEmail
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 sendCancellationEmail
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 getProcessById
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 getScopesByProviderId
77.78% covered (warning)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
4.18
1<?php
2
3declare(strict_types=1);
4
5namespace BO\Zmscitizenapi\Services\Core;
6
7use BO\Zmscitizenapi\Helper\ClientIpHelper;
8use BO\Zmscitizenapi\Services\Core\LoggerService;
9use BO\Zmscitizenapi\Services\Core\ExceptionService;
10use BO\Zmsentities\Calendar;
11use BO\Zmsentities\Process;
12use BO\Zmsentities\Source;
13use BO\Zmsentities\Collection\DayList;
14use BO\Zmsentities\Collection\ProcessList;
15use BO\Zmsentities\Collection\ProviderList;
16use BO\Zmsentities\Collection\RequestList;
17use BO\Zmsentities\Collection\RequestRelationList;
18use BO\Zmsentities\Collection\ScopeList;
19
20class ZmsApiClientService
21{
22    private static function fetchSourceData(): Source
23    {
24        $cacheKey = 'source_' . \App::$source_name;
25        if (\App::$cache && ($data = \App::$cache->get($cacheKey))) {
26            return $data;
27        }
28
29        $result = \App::$http->readGetResult('/source/' . \App::$source_name . '/', [
30            'resolveReferences' => 2,
31        ]);
32        $entity = $result?->getEntity();
33        if (!$entity instanceof Source) {
34            return new Source();
35        }
36
37        if (\App::$cache) {
38            \App::$cache->set($cacheKey, $entity, \App::$SOURCE_CACHE_TTL);
39            LoggerService::logInfo('Cache set', [
40                'key' => $cacheKey,
41                'ttl' => \App::$SOURCE_CACHE_TTL,
42                'entity_type' => get_class($entity)
43            ]);
44        }
45
46        return $entity;
47    }
48
49    public static function getOffices(): ProviderList
50    {
51        try {
52            $sources = self::fetchSourceData();
53            $list = $sources?->getProviderList();
54            if (!$list instanceof ProviderList) {
55                return new ProviderList();
56            }
57            return $list;
58        } catch (\Exception $e) {
59            ExceptionService::handleException($e);
60        }
61    }
62
63    public static function getScopes(): ScopeList
64    {
65        try {
66            $sources = self::fetchSourceData();
67            $list = $sources?->getScopeList();
68            if (!$list instanceof ScopeList) {
69                return new ScopeList();
70            }
71            return $list;
72        } catch (\Exception $e) {
73            ExceptionService::handleException($e);
74        }
75    }
76
77    public static function getServices(): RequestList
78    {
79        try {
80            $sources = self::fetchSourceData();
81            $list = $sources?->getRequestList();
82            if (!$list instanceof RequestList) {
83                return new RequestList();
84            }
85            return $list;
86        } catch (\Exception $e) {
87            ExceptionService::handleException($e);
88        }
89    }
90
91    public static function getRequestRelationList(): RequestRelationList
92    {
93        try {
94            $sources = self::fetchSourceData();
95            $list = $sources?->getRequestRelationList();
96            if (!$list instanceof RequestRelationList) {
97                return new RequestRelationList();
98            }
99            return $list;
100        } catch (\Exception $e) {
101            ExceptionService::handleException($e);
102        }
103    }
104
105    public static function getFreeDays(ProviderList $providers, RequestList $requests, array $firstDay, array $lastDay): Calendar
106    {
107        try {
108            $calendar = new Calendar();
109            $calendar->firstDay = $firstDay;
110            $calendar->lastDay = $lastDay;
111            $calendar->providers = $providers;
112            $calendar->requests = $requests;
113            $result = \App::$http->readPostResult('/calendar/', $calendar);
114            $entity = $result?->getEntity();
115
116            if (!$entity instanceof Calendar) {
117                return new Calendar();
118            }
119
120            return $entity;
121        } catch (\Exception $e) {
122            ExceptionService::handleException($e);
123        }
124    }
125
126    public static function getFreeTimeslots(ProviderList $providers, RequestList $requests, array $firstDay, array $lastDay): ProcessList
127    {
128        try {
129            $calendar = new Calendar();
130            $calendar->firstDay = $firstDay;
131            $calendar->lastDay = $lastDay;
132            $calendar->providers = $providers;
133            $calendar->requests = $requests;
134            $result = \App::$http->readPostResult('/process/status/free/unique/', $calendar);
135            $collection = $result?->getCollection();
136            if (!$collection instanceof ProcessList) {
137                return new ProcessList();
138            }
139
140            return $collection;
141        } catch (\Exception $e) {
142            ExceptionService::handleException($e);
143        }
144    }
145
146    public static function reserveTimeslot(Process $appointmentProcess, array $serviceIds, array $serviceCounts): Process
147    {
148        try {
149            $requests = [];
150            foreach ($serviceIds as $index => $serviceId) {
151                $count = intval($serviceCounts[$index]);
152                for ($i = 0; $i < $count; $i++) {
153                    $requests[] = [
154                        'id' => $serviceId,
155                        'source' => \App::$source_name
156                    ];
157                }
158            }
159
160            $processEntity = new Process();
161            $processEntity->appointments = $appointmentProcess->appointments ?? [];
162            $processEntity->authKey = $appointmentProcess->authKey ?? null;
163            $processEntity->clients = $appointmentProcess->clients ?? [];
164            $processEntity->scope = $appointmentProcess->scope ?? null;
165            $processEntity->requests = $requests;
166            $processEntity->lastChange = $appointmentProcess->lastChange ?? time();
167            $processEntity->createIP = ClientIpHelper::getClientIp();
168            $processEntity->createTimestamp = time();
169            if (isset($appointmentProcess->queue)) {
170                $processEntity->queue = $appointmentProcess->queue;
171            }
172
173            $result = \App::$http->readPostResult('/process/status/reserved/', $processEntity);
174            $entity = $result?->getEntity();
175            if (!$entity instanceof Process) {
176                return new Process();
177            }
178            return $entity;
179        } catch (\Exception $e) {
180            ExceptionService::handleException($e);
181        }
182    }
183
184    public static function submitClientData(Process $process): Process
185    {
186        try {
187            $url = "/process/{$process->id}/{$process->authKey}/";
188            $result = \App::$http->readPostResult($url, $process);
189            $entity = $result?->getEntity();
190            if (!$entity instanceof Process) {
191                return new Process();
192            }
193            return $entity;
194        } catch (\Exception $e) {
195            ExceptionService::handleException($e);
196        }
197    }
198
199    public static function preconfirmProcess(Process $process): Process
200    {
201        try {
202            $url = '/process/status/preconfirmed/';
203            $result = \App::$http->readPostResult($url, $process);
204            $entity = $result?->getEntity();
205            if (!$entity instanceof Process) {
206                return new Process();
207            }
208            return $entity;
209        } catch (\Exception $e) {
210            ExceptionService::handleException($e);
211        }
212    }
213
214    public static function confirmProcess(Process $process): Process
215    {
216        try {
217            $url = '/process/status/confirmed/';
218            $result = \App::$http->readPostResult($url, $process);
219            $entity = $result?->getEntity();
220            if (!$entity instanceof Process) {
221                return new Process();
222            }
223            return $entity;
224        } catch (\Exception $e) {
225            ExceptionService::handleException($e);
226        }
227    }
228
229    public static function cancelAppointment(Process $process): Process
230    {
231        try {
232            $url = "/process/{$process->id}/{$process->authKey}/";
233            $result = \App::$http->readDeleteResult($url, []);
234            $entity = $result?->getEntity();
235            if (!$entity instanceof Process) {
236                return new Process();
237            }
238            return $entity;
239        } catch (\Exception $e) {
240            ExceptionService::handleException($e);
241        }
242    }
243
244    public static function sendConfirmationEmail(Process $process): Process
245    {
246        try {
247            $url = "/process/{$process->id}/{$process->authKey}/confirmation/mail/";
248            $result = \App::$http->readPostResult($url, $process);
249            $entity = $result?->getEntity();
250            if (!$entity instanceof Process) {
251                return new Process();
252            }
253            return $entity;
254        } catch (\Exception $e) {
255            ExceptionService::handleException($e);
256        }
257    }
258
259    public static function sendPreconfirmationEmail(Process $process): Process
260    {
261        try {
262            $url = "/process/{$process->id}/{$process->authKey}/preconfirmation/mail/";
263            $result = \App::$http->readPostResult($url, $process);
264            $entity = $result?->getEntity();
265            if (!$entity instanceof Process) {
266                return new Process();
267            }
268            return $entity;
269        } catch (\Exception $e) {
270            ExceptionService::handleException($e);
271        }
272    }
273
274    public static function sendCancellationEmail(Process $process): Process
275    {
276        try {
277            $url = "/process/{$process->id}/{$process->authKey}/delete/mail/";
278            $result = \App::$http->readPostResult($url, $process);
279            $entity = $result?->getEntity();
280            if (!$entity instanceof Process) {
281                return new Process();
282            }
283            return $entity;
284        } catch (\Exception $e) {
285            ExceptionService::handleException($e);
286        }
287    }
288
289    public static function getProcessById(int $processId, string $authKey): Process
290    {
291        try {
292            $resolveReferences = 2;
293            $result = \App::$http->readGetResult("/process/{$processId}/{$authKey}/", [
294                'resolveReferences' => $resolveReferences
295            ]);
296            $entity = $result?->getEntity();
297            if (!$entity instanceof Process) {
298                return new Process();
299            }
300            return $entity;
301        } catch (\Exception $e) {
302            ExceptionService::handleException($e);
303        }
304    }
305
306    public static function getScopesByProviderId(string $source, string|int $providerId): ScopeList
307    {
308        try {
309            $scopeList = self::getScopes();
310            if (!$scopeList instanceof ScopeList) {
311                return new ScopeList();
312            }
313            $result = $scopeList->withProviderID($source, (string) $providerId);
314            if (!$result instanceof ScopeList) {
315                return new ScopeList();
316            }
317            return $result;
318        } catch (\Exception $e) {
319            ExceptionService::handleException($e);
320        }
321    }
322}