Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.31% covered (success)
92.31%
252 / 273
50.00% covered (danger)
50.00%
6 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
MapperService
92.31% covered (success)
92.31%
252 / 273
50.00% covered (danger)
50.00%
6 / 12
148.92
0.00% covered (danger)
0.00%
0 / 1
 mapScopeForProvider
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 mapOfficesWithScope
95.12% covered (success)
95.12%
39 / 41
0.00% covered (danger)
0.00%
0 / 1
37
 mapCombinable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 mapServicesWithCombinations
96.00% covered (success)
96.00%
24 / 25
0.00% covered (danger)
0.00%
0 / 1
10
 mapRelations
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
4.01
 scopeToThinnedScope
90.91% covered (success)
90.91%
30 / 33
0.00% covered (danger)
0.00%
0 / 1
21.33
 processToThinnedProcess
81.40% covered (warning)
81.40%
35 / 43
0.00% covered (danger)
0.00%
0 / 1
45.82
 thinnedProcessToProcess
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
4
 createScope
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
1 / 1
6
 createRequests
66.67% covered (warning)
66.67%
12 / 18
0.00% covered (danger)
0.00%
0 / 1
4.59
 contactToThinnedContact
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
2
 providerToThinnedProvider
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
8
1<?php
2
3declare(strict_types=1);
4
5namespace BO\Zmscitizenapi\Services\Core;
6
7use BO\Zmscitizenapi\Helper\ClientIpHelper;
8use BO\Zmscitizenapi\Models\Office;
9use BO\Zmscitizenapi\Models\Combinable;
10use BO\Zmscitizenapi\Models\OfficeServiceRelation;
11use BO\Zmscitizenapi\Models\Service;
12use BO\Zmscitizenapi\Models\ThinnedContact;
13use BO\Zmscitizenapi\Models\ThinnedProcess;
14use BO\Zmscitizenapi\Models\ThinnedProvider;
15use BO\Zmscitizenapi\Models\ThinnedScope;
16use BO\Zmscitizenapi\Models\Collections\OfficeList;
17use BO\Zmscitizenapi\Models\Collections\OfficeServiceRelationList;
18use BO\Zmscitizenapi\Models\Collections\ServiceList;
19use BO\Zmscitizenapi\Models\Collections\ThinnedScopeList;
20use BO\Zmsentities\Appointment;
21use BO\Zmsentities\Client;
22use BO\Zmsentities\Contact;
23use BO\Zmsentities\Process;
24use BO\Zmsentities\Provider;
25use BO\Zmsentities\Request;
26use BO\Zmsentities\Scope;
27use BO\Zmsentities\Collection\ProviderList;
28use BO\Zmsentities\Collection\RequestList;
29use BO\Zmsentities\Collection\RequestRelationList;
30
31/**
32 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
33 * @TODO: Extract class has ExcessiveClassComplexity 101 vs 100
34 */
35class MapperService
36{
37    public static function mapScopeForProvider(int $providerId, ?ThinnedScopeList $scopes): ThinnedScope
38    {
39        if (!$scopes) {
40            return new ThinnedScope();
41        }
42
43        $matchingScope = new ThinnedScope();
44        foreach ($scopes->getScopes() as $scope) {
45            if ($scope->provider && $scope->provider->id === $providerId) {
46                $matchingScope = $scope;
47                break;
48            }
49        }
50
51        return $matchingScope;
52    }
53
54    /**
55     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
56     * @SuppressWarnings(PHPMD.NPathComplexity)
57     * @TODO: Extract mapping logic into specialized mapper classes for each entity type
58     */
59    public static function mapOfficesWithScope(ProviderList $providerList, bool $showUnpublished = false): OfficeList
60    {
61        $offices = [];
62        $scopes = ZmsApiFacadeService::getScopes();
63        if (!$scopes instanceof ThinnedScopeList) {
64            return new OfficeList();
65        }
66
67        foreach ($providerList as $provider) {
68            $providerScope = self::mapScopeForProvider((int) $provider->id, $scopes);
69
70            if (!$showUnpublished && isset($provider->data['public']) && !(bool) $provider->data['public']) {
71                continue;
72            }
73
74            $offices[] = new Office(
75                id: isset($provider->id) ? (int) $provider->id : 0,
76                name: isset($provider->displayName) ? $provider->displayName : (isset($provider->name) ? $provider->name : null),
77                address: isset($provider->data['address']) ? $provider->data['address'] : null,
78                showAlternativeLocations: isset($provider->data['showAlternativeLocations']) ? $provider->data['showAlternativeLocations'] : null,
79                displayNameAlternatives: $provider->data['displayNameAlternatives'] ?? [],
80                organization: $provider->data['organization'] ?? null,
81                organizationUnit: $provider->data['organizationUnit'] ?? null,
82                slotTimeInMinutes: $provider->data['slotTimeInMinutes'] ?? null,
83                geo: isset($provider->data['geo']) ? $provider->data['geo'] : null,
84                disabledByServices: isset($provider->data['dontShowByServices']) ? $provider->data['dontShowByServices'] : [],
85                priority: isset($provider->data['prio']) ? $provider->data['prio'] : 1,
86                scope: isset($providerScope) && !isset($providerScope['errors']) ? new ThinnedScope(
87                    id: isset($providerScope->id) ? (int) $providerScope->id : 0,
88                    provider: isset($providerScope->provider) ? $providerScope->provider : null,
89                    shortName: isset($providerScope->shortName) ? (string) $providerScope->shortName : null,
90                    emailFrom: isset($providerScope->emailFrom) ? (string) $providerScope->emailFrom : null,
91                    emailRequired: isset($providerScope->emailRequired) ? (bool) $providerScope->emailRequired : null,
92                    telephoneActivated: isset($providerScope->telephoneActivated) ? (bool) $providerScope->telephoneActivated : null,
93                    telephoneRequired: isset($providerScope->telephoneRequired) ? (bool) $providerScope->telephoneRequired : null,
94                    customTextfieldActivated: isset($providerScope->customTextfieldActivated) ? (bool) $providerScope->customTextfieldActivated : null,
95                    customTextfieldRequired: isset($providerScope->customTextfieldRequired) ? (bool) $providerScope->customTextfieldRequired : null,
96                    customTextfieldLabel: isset($providerScope->customTextfieldLabel) ? (string) $providerScope->customTextfieldLabel : null,
97                    customTextfield2Activated: isset($providerScope->customTextfield2Activated) ? (bool) $providerScope->customTextfield2Activated : null,
98                    customTextfield2Required: isset($providerScope->customTextfield2Required) ? (bool) $providerScope->customTextfield2Required : null,
99                    customTextfield2Label: isset($providerScope->customTextfield2Label) ? (string) $providerScope->customTextfield2Label : null,
100                    captchaActivatedRequired: isset($providerScope->captchaActivatedRequired) ? (bool) $providerScope->captchaActivatedRequired : null,
101                    displayInfo: isset($providerScope->displayInfo) ? (string) $providerScope->displayInfo : null,
102                    slotsPerAppointment: isset($providerScope->slotsPerAppointment) ? ((string) $providerScope->slotsPerAppointment === '' ? null : (string) $providerScope->slotsPerAppointment) : null
103                ) : null,
104                maxSlotsPerAppointment: isset($providerScope) && !isset($providerScope['errors']) && isset($providerScope->slotsPerAppointment) ? ((string) $providerScope->slotsPerAppointment === '' ? null : (string) $providerScope->slotsPerAppointment) : null
105            );
106        }
107
108        return new OfficeList($offices);
109    }
110
111    public static function mapCombinable(array $serviceCombinations): ?Combinable
112    {
113        return !empty($serviceCombinations) ? new Combinable($serviceCombinations) : null;
114    }
115
116    /**
117     * Map services with combinations based on request and relation lists.
118     *
119     * @param RequestList $requestList
120     * @param RequestRelationList $relationList
121     * @return ServiceList
122     */
123    public static function mapServicesWithCombinations(
124        RequestList $requestList,
125        RequestRelationList $relationList,
126        bool $showUnpublished = false
127    ): ServiceList {
128        /** @var array<string, array<int>> $servicesProviderIds */
129        $servicesProviderIds = [];
130        foreach ($relationList as $relation) {
131            if (!$showUnpublished && !$relation->isPublic()) {
132                continue;
133            }
134
135            $serviceId = $relation->request->id;
136            $servicesProviderIds[$serviceId] ??= [];
137            $servicesProviderIds[$serviceId][] = $relation->provider->id;
138        }
139
140        /** @var Service[] $services */
141        $services = [];
142        $requestArray = iterator_to_array($requestList);
143        usort($requestArray, function ($a, $b) {
144
145            return $a->getId() <=> $b->getId();
146            // Sorting by service ID (ascending order)
147        });
148        foreach ($requestArray as $service) {
149            if (
150                !$showUnpublished
151                && isset($service->getAdditionalData()['public'])
152                && !$service->getAdditionalData()['public']
153            ) {
154                continue;
155            }
156
157            /** @var array<string, array<int>> $serviceCombinations */
158            $serviceCombinations = [];
159            $combinableData = $service->getAdditionalData()['combinable'] ?? [];
160            foreach ($combinableData as $combinationServiceId) {
161                $commonProviders = array_intersect($servicesProviderIds[$service->getId()] ?? [], $servicesProviderIds[$combinationServiceId] ?? []);
162                $serviceCombinations[$combinationServiceId] = !empty($commonProviders) ? array_values($commonProviders) : [];
163            }
164
165            $combinable = self::mapCombinable($serviceCombinations);
166            $services[] = new Service(id: (int) $service->getId(), name: $service->getName(), maxQuantity: $service->getAdditionalData()['maxQuantity'] ?? 1, combinable: $combinable ?? new Combinable());
167        }
168
169        return new ServiceList($services);
170    }
171
172
173    public static function mapRelations(
174        RequestRelationList $relationList,
175        bool $showUnpublished = false
176    ): OfficeServiceRelationList {
177        $relations = [];
178        foreach ($relationList as $relation) {
179            if (!$showUnpublished && !$relation->isPublic()) {
180                continue;
181            }
182
183            $relations[] = new OfficeServiceRelation(
184                officeId: (int) $relation->provider->id,
185                serviceId: (int) $relation->request->id,
186                slots: intval($relation->slots),
187                public: (bool) $relation->isPublic(),
188                maxQuantity: (int) $relation->maxQuantity
189            );
190        }
191
192        return new OfficeServiceRelationList($relations);
193    }
194
195    /**
196     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
197     * @SuppressWarnings(PHPMD.NPathComplexity)
198     */
199    public static function scopeToThinnedScope(Scope $scope): ThinnedScope
200    {
201        if (!$scope || !isset($scope->id)) {
202            return new ThinnedScope();
203        }
204
205        $thinnedProvider = null;
206        try {
207            if (isset($scope->provider)) {
208                $provider = $scope->provider;
209                $contact = $provider->contact ?? null;
210                $thinnedProvider = new ThinnedProvider(
211                    id: isset($provider->id) ? (int)$provider->id : null,
212                    name: $provider->name ?? null,
213                    displayName: $provider->displayName ?? null,
214                    source: $provider->source ?? null,
215                    contact: $contact ? self::contactToThinnedContact($contact) : null
216                );
217            }
218        } catch (\BO\Zmsentities\Exception\ScopeMissingProvider $e) {
219            $thinnedProvider = null;
220        }
221
222        return new ThinnedScope(
223            id: (int) ($scope->id ?? 0),
224            provider: $thinnedProvider,
225            shortName: isset($scope->shortName) ? (string) $scope->shortName : null,
226            emailFrom: (string) $scope->getEmailFrom() ?? null,
227            emailRequired: isset($scope->data['emailRequired']) ? (bool) $scope->data['emailRequired'] : null,
228            telephoneActivated: isset($scope->data['telephoneActivated']) ? (bool) $scope->data['telephoneActivated'] : null,
229            telephoneRequired: isset($scope->data['telephoneRequired']) ? (bool) $scope->data['telephoneRequired'] : null,
230            customTextfieldActivated: isset($scope->data['customTextfieldActivated']) ? (bool) $scope->data['customTextfieldActivated'] : null,
231            customTextfieldRequired: isset($scope->data['customTextfieldRequired']) ? (bool) $scope->data['customTextfieldRequired'] : null,
232            customTextfieldLabel: isset($scope->data['customTextfieldLabel']) ? (string) $scope->data['customTextfieldLabel'] : null,
233            customTextfield2Activated: isset($scope->data['customTextfield2Activated']) ? (bool) $scope->data['customTextfield2Activated'] : null,
234            customTextfield2Required: isset($scope->data['customTextfield2Required']) ? (bool) $scope->data['customTextfield2Required'] : null,
235            customTextfield2Label: isset($scope->data['customTextfield2Label']) ? (string) $scope->data['customTextfield2Label'] : null,
236            captchaActivatedRequired: isset($scope->data['captchaActivatedRequired']) ? (bool) $scope->data['captchaActivatedRequired'] : null,
237            displayInfo: isset($scope->data['displayInfo']) ? (string) $scope->data['displayInfo'] : null,
238            slotsPerAppointment: isset($scope->data['slotsPerAppointment']) ? ((string) $scope->data['slotsPerAppointment'] === '' ? null : (string) $scope->data['slotsPerAppointment']) : null
239        );
240    }
241
242    /**
243     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
244     * @SuppressWarnings(PHPMD.NPathComplexity)
245     * @TODO: Break down process mapping into smaller, focused methods
246     */
247    public static function processToThinnedProcess(Process $myProcess): ThinnedProcess
248    {
249        if (!$myProcess || !isset($myProcess->id)) {
250            return new ThinnedProcess();
251        }
252
253        $subRequestCounts = [];
254        $mainServiceId = null;
255        $mainServiceName = null;
256        $mainServiceCount = 0;
257        $requests = $myProcess->getRequests() ?? [];
258        if ($requests) {
259            $requests = is_array($requests) ? $requests : iterator_to_array($requests);
260            if (count($requests) > 0) {
261                $mainServiceId = $requests[0]->id;
262                foreach ($requests as $request) {
263                    if ($request->id === $mainServiceId) {
264                        $mainServiceCount++;
265                        if (!$mainServiceName && isset($request->name)) {
266                            $mainServiceName = $request->name;
267                        }
268                    } else {
269                        if (!isset($subRequestCounts[$request->id])) {
270                            $subRequestCounts[$request->id] = [
271                                'id' => (int) $request->id,
272                                'name'  => $request->name,
273                                'count' => 0,
274                            ];
275                        }
276                        $subRequestCounts[$request->id]['count']++;
277                    }
278                }
279            }
280        }
281
282        return new ThinnedProcess(
283            processId: isset($myProcess->id) ? (int) $myProcess->id : 0,
284            timestamp: (isset($myProcess->appointments[0]) && isset($myProcess->appointments[0]->date)) ? strval($myProcess->appointments[0]->date) : null,
285            authKey: isset($myProcess->authKey) ? $myProcess->authKey : null,
286            captchaToken: isset($myProcess->captchaToken) ? $myProcess->captchaToken : null,
287            familyName: (isset($myProcess->clients[0]) && isset($myProcess->clients[0]->familyName)) ? $myProcess->clients[0]->familyName : null,
288            customTextfield: isset($myProcess->customTextfield) ? $myProcess->customTextfield : null,
289            customTextfield2: isset($myProcess->customTextfield2) ? $myProcess->customTextfield2 : null,
290            email: (isset($myProcess->clients[0]) && isset($myProcess->clients[0]->email)) ? $myProcess->clients[0]->email : null,
291            telephone: (isset($myProcess->clients[0]) && isset($myProcess->clients[0]->telephone)) ? $myProcess->clients[0]->telephone : null,
292            officeName: (isset($myProcess->scope->contact) && isset($myProcess->scope->contact->name)) ? $myProcess->scope->contact->name : null,
293            officeId: (isset($myProcess->scope->provider) && isset($myProcess->scope->provider->id)) ? (int) $myProcess->scope->provider->id : 0,
294            scope: isset($myProcess->scope) ? self::scopeToThinnedScope($myProcess->scope) : null,
295            subRequestCounts: isset($subRequestCounts) ? array_values($subRequestCounts) : [],
296            serviceId: isset($mainServiceId) ? (int) $mainServiceId : 0,
297            serviceName: isset($mainServiceName) ? $mainServiceName : null,
298            serviceCount: isset($mainServiceCount) ? $mainServiceCount : 0,
299            status: (isset($myProcess->queue) && isset($myProcess->queue->status)) ? $myProcess->queue->status : null,
300            slotCount: (isset($myProcess->appointments[0]) && isset($myProcess->appointments[0]->slotCount)) ? (int) $myProcess->appointments[0]->slotCount : null
301        );
302    }
303
304    public static function thinnedProcessToProcess(ThinnedProcess $thinnedProcess): Process
305    {
306        if (!$thinnedProcess || !isset($thinnedProcess->processId)) {
307            return new Process();
308        }
309
310        $processEntity = new Process();
311        $processEntity->id = $thinnedProcess->processId;
312        $processEntity->authKey = $thinnedProcess->authKey ?? null;
313        $processEntity->customTextfield = $thinnedProcess->customTextfield ?? null;
314        $processEntity->customTextfield2 = $thinnedProcess->customTextfield2 ?? null;
315        $processEntity->captchaToken = $thinnedProcess->captchaToken ?? null;
316
317        $client = new Client();
318        $client->familyName = $thinnedProcess->familyName ?? null;
319        $client->email = $thinnedProcess->email ?? null;
320        $client->telephone = $thinnedProcess->telephone ?? null;
321        $processEntity->clients = [$client];
322
323        $appointment = new Appointment();
324        $appointment->slotCount = $thinnedProcess->slotCount ?? null;
325        $appointment->date = $thinnedProcess->timestamp ?? null;
326        $processEntity->appointments = [$appointment];
327        $processEntity->scope = self::createScope($thinnedProcess);
328        $processEntity->requests = self::createRequests($thinnedProcess);
329
330        if (isset($thinnedProcess->status)) {
331            $processEntity->queue = new \stdClass();
332            $processEntity->queue->status = $thinnedProcess->status;
333            $processEntity->status = $thinnedProcess->status;
334        }
335
336        $processEntity->lastChange = time();
337        $processEntity->createIP = ClientIpHelper::getClientIp();
338        $processEntity->createTimestamp = time();
339        return $processEntity;
340    }
341
342    private static function createScope(ThinnedProcess $thinnedProcess): Scope
343    {
344        $scope = new Scope();
345        if ($thinnedProcess->scope) {
346            $scope->id = $thinnedProcess->scope->id;
347            $scope->source = \App::$source_name;
348
349            // Set preferences as array structure
350            $scope->preferences = [
351                'client' => [
352                    'emailFrom' => $thinnedProcess->scope->getEmailFrom() ?? null,
353                    'emailRequired' => $thinnedProcess->scope->getEmailRequired() ?? false,
354                    'telephoneActivated' => $thinnedProcess->scope->getTelephoneActivated() ?? false,
355                    'telephoneRequired' => $thinnedProcess->scope->getTelephoneRequired() ?? false,
356                    'customTextfieldActivated' => $thinnedProcess->scope->getCustomTextfieldActivated() ?? false,
357                    'customTextfieldRequired' => $thinnedProcess->scope->getCustomTextfieldRequired() ?? false,
358                    'customTextfieldLabel' => $thinnedProcess->scope->getCustomTextfieldLabel() ?? null,
359                    'customTextfield2Activated' => $thinnedProcess->scope->getCustomTextfield2Activated() ?? false,
360                    'customTextfield2Required' => $thinnedProcess->scope->getCustomTextfield2Required() ?? false,
361                    'customTextfield2Label' => $thinnedProcess->scope->getCustomTextfield2Label() ?? null
362                ],
363                'notifications' => [
364                    'enabled' => true
365                ]
366            ];
367        }
368        if (isset($thinnedProcess->officeName)) {
369            $scope->contact = new Contact();
370            $scope->contact->name = $thinnedProcess->officeName;
371        }
372        if (isset($thinnedProcess->officeId)) {
373            $scope->provider = new Provider();
374            $scope->provider->id = $thinnedProcess->officeId;
375            if (isset($thinnedProcess->scope->provider)) {
376                $provider = $thinnedProcess->scope->provider;
377                $scope->provider->name  = $provider->name ?? null;
378                $scope->provider->displayName = $provider->displayName ?? null;
379
380                if (isset($provider->contact)) {
381                    $scope->provider->contact = new Contact();
382                    $scope->provider->contact->street = $provider->contact->street ?? null;
383                    $scope->provider->contact->streetNumber = $provider->contact->streetNumber ?? null;
384                }
385            }
386            $scope->provider->source = \App::$source_name;
387        }
388
389        return $scope;
390    }
391
392    private static function createRequests(ThinnedProcess $thinnedProcess): array
393    {
394        $requests = [];
395        $mainServiceId = $thinnedProcess->serviceId ?? null;
396        $mainServiceName = $thinnedProcess->serviceName ?? null;
397        $mainServiceCount = $thinnedProcess->serviceCount ?? 0;
398
399        for ($i = 0; $i < $mainServiceCount; $i++) {
400            $request = new Request();
401            $request->id = $mainServiceId;
402            $request->name = $mainServiceName;
403            $request->source = \App::$source_name;
404            $requests[] = $request;
405        }
406
407        foreach ($thinnedProcess->subRequestCounts ?? [] as $subRequest) {
408            for ($i = 0; $i < ($subRequest['count'] ?? 0); $i++) {
409                $request = new Request();
410                $request->id = $subRequest['id'];
411                $request->name = $subRequest['name'];
412                $request->source = \App::$source_name;
413                $requests[] = $request;
414            }
415        }
416
417        return $requests;
418    }
419
420    /**
421     * Converts a raw or existing contact object/array into a ThinnedContact model.
422     *
423     * @param object|array $contact
424     * @return ThinnedContact
425     */
426    public static function contactToThinnedContact($contact): ThinnedContact
427    {
428        if (is_array($contact)) {
429            return new ThinnedContact(
430                city: $contact['city'] ?? null,
431                country: $contact['country'] ?? null,
432                name: $contact['name'] ?? null,
433                postalCode: $contact['postalCode'] ?? null,
434                region: $contact['region'] ?? null,
435                street: $contact['street'] ?? null,
436                streetNumber: $contact['streetNumber'] ?? null
437            );
438        }
439
440        return new ThinnedContact(
441            city: $contact->city ?? null,
442            country: $contact->country ?? null,
443            name: $contact->name ?? null,
444            postalCode: $contact->postalCode ?? null,
445            region: $contact->region ?? null,
446            street: $contact->street ?? null,
447            streetNumber: $contact->streetNumber ?? null
448        );
449    }
450
451    /**
452     * Convert a Provider object to a ThinnedProvider.
453     *
454     * @param Provider $provider
455     * @return ThinnedProvider
456     */
457    public static function providerToThinnedProvider(Provider $provider): ThinnedProvider
458    {
459        return new ThinnedProvider(
460            id: isset($provider->id) ? (int) $provider->id : null,
461            name: isset($provider->name) ? $provider->name : null,
462            displayName: isset($provider->displayName) ? $provider->displayName : null,
463            source: isset($provider->source) ? $provider->source : null,
464            lon: isset($provider->data['geo']['lon']) ? (float) $provider->data['geo']['lon'] : null,
465            lat: isset($provider->data['geo']['lat']) ? (float) $provider->data['geo']['lat'] : null,
466            contact: isset($provider->contact) ? self::contactToThinnedContact($provider->contact) : null
467        );
468    }
469}