Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.33% |
179 / 196 |
|
60.00% |
6 / 10 |
CRAP | |
0.00% |
0 / 1 |
MapperService | |
91.33% |
179 / 196 |
|
60.00% |
6 / 10 |
112.19 | |
0.00% |
0 / 1 |
mapScopeForProvider | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
mapOfficesWithScope | |
94.12% |
32 / 34 |
|
0.00% |
0 / 1 |
26.14 | |||
mapCombinable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
mapServicesWithCombinations | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
8 | |||
mapRelations | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
scopeToThinnedScope | |
86.96% |
20 / 23 |
|
0.00% |
0 / 1 |
13.38 | |||
processToThinnedProcess | |
80.00% |
28 / 35 |
|
0.00% |
0 / 1 |
37.20 | |||
thinnedProcessToProcess | |
92.19% |
59 / 64 |
|
0.00% |
0 / 1 |
10.05 | |||
contactToThinnedContact | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
providerToThinnedProvider | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
7 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace BO\Zmscitizenapi\Services\Core; |
6 | |
7 | use BO\Zmscitizenapi\Helper\ClientIpHelper; |
8 | use BO\Zmscitizenapi\Models\Office; |
9 | use BO\Zmscitizenapi\Models\Combinable; |
10 | use BO\Zmscitizenapi\Models\OfficeServiceRelation; |
11 | use BO\Zmscitizenapi\Models\Service; |
12 | use BO\Zmscitizenapi\Models\ThinnedContact; |
13 | use BO\Zmscitizenapi\Models\ThinnedProcess; |
14 | use BO\Zmscitizenapi\Models\ThinnedProvider; |
15 | use BO\Zmscitizenapi\Models\ThinnedScope; |
16 | use BO\Zmscitizenapi\Models\Collections\OfficeList; |
17 | use BO\Zmscitizenapi\Models\Collections\OfficeServiceRelationList; |
18 | use BO\Zmscitizenapi\Models\Collections\ServiceList; |
19 | use BO\Zmscitizenapi\Models\Collections\ThinnedScopeList; |
20 | use BO\Zmsentities\Appointment; |
21 | use BO\Zmsentities\Client; |
22 | use BO\Zmsentities\Contact; |
23 | use BO\Zmsentities\Process; |
24 | use BO\Zmsentities\Provider; |
25 | use BO\Zmsentities\Request; |
26 | use BO\Zmsentities\Scope; |
27 | use BO\Zmsentities\Collection\ProviderList; |
28 | use BO\Zmsentities\Collection\RequestList; |
29 | use BO\Zmsentities\Collection\RequestRelationList; |
30 | |
31 | /** |
32 | * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) |
33 | * @TODO: Extract class has ExcessiveClassComplexity 101 vs 100 |
34 | */ |
35 | class 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 | scope: isset($providerScope) && !isset($providerScope['errors']) ? new ThinnedScope( |
85 | id: isset($providerScope->id) ? (int) $providerScope->id : 0, |
86 | provider: isset($providerScope->provider) ? $providerScope->provider : null, |
87 | shortName: isset($providerScope->shortName) ? $providerScope->shortName : null, |
88 | emailFrom: isset($providerScope->emailFrom) ? $providerScope->emailFrom : null, |
89 | emailRequired: isset($providerScope->emailRequired) ? (bool) $providerScope->emailRequired : null, |
90 | telephoneActivated: isset($providerScope->telephoneActivated) ? (bool) $providerScope->telephoneActivated : null, |
91 | telephoneRequired: isset($providerScope->telephoneRequired) ? (bool) $providerScope->telephoneRequired : null, |
92 | customTextfieldActivated: isset($providerScope->customTextfieldActivated) ? (bool) $providerScope->customTextfieldActivated : null, |
93 | customTextfieldRequired: isset($providerScope->customTextfieldRequired) ? (bool) $providerScope->customTextfieldRequired : null, |
94 | customTextfieldLabel: isset($providerScope->customTextfieldLabel) ? $providerScope->customTextfieldLabel : null, |
95 | captchaActivatedRequired: isset($providerScope->captchaActivatedRequired) ? (bool) $providerScope->captchaActivatedRequired : null, |
96 | displayInfo: isset($providerScope->displayInfo) ? $providerScope->displayInfo : null |
97 | ) : null |
98 | ); |
99 | } |
100 | |
101 | return new OfficeList($offices); |
102 | } |
103 | |
104 | public static function mapCombinable(array $serviceCombinations): ?Combinable |
105 | { |
106 | return !empty($serviceCombinations) ? new Combinable($serviceCombinations) : null; |
107 | } |
108 | |
109 | /** |
110 | * Map services with combinations based on request and relation lists. |
111 | * |
112 | * @param RequestList $requestList |
113 | * @param RequestRelationList $relationList |
114 | * @return ServiceList |
115 | */ |
116 | public static function mapServicesWithCombinations( |
117 | RequestList $requestList, |
118 | RequestRelationList $relationList, |
119 | bool $showUnpublished = false |
120 | ): ServiceList { |
121 | /** @var array<string, array<int>> $servicesProviderIds */ |
122 | $servicesProviderIds = []; |
123 | foreach ($relationList as $relation) { |
124 | $serviceId = $relation->request->id; |
125 | $servicesProviderIds[$serviceId] ??= []; |
126 | $servicesProviderIds[$serviceId][] = $relation->provider->id; |
127 | } |
128 | |
129 | /** @var Service[] $services */ |
130 | $services = []; |
131 | $requestArray = iterator_to_array($requestList); |
132 | usort($requestArray, function ($a, $b) { |
133 | |
134 | return $a->getId() <=> $b->getId(); |
135 | // Sorting by service ID (ascending order) |
136 | }); |
137 | foreach ($requestArray as $service) { |
138 | if ( |
139 | !$showUnpublished |
140 | && isset($service->getAdditionalData()['public']) |
141 | && !$service->getAdditionalData()['public'] |
142 | ) { |
143 | continue; |
144 | } |
145 | |
146 | /** @var array<string, array<int>> $serviceCombinations */ |
147 | $serviceCombinations = []; |
148 | $combinableData = $service->getAdditionalData()['combinable'] ?? []; |
149 | foreach ($combinableData as $combinationServiceId) { |
150 | $commonProviders = array_intersect($servicesProviderIds[$service->getId()] ?? [], $servicesProviderIds[$combinationServiceId] ?? []); |
151 | $serviceCombinations[$combinationServiceId] = !empty($commonProviders) ? array_values($commonProviders) : []; |
152 | } |
153 | |
154 | $combinable = self::mapCombinable($serviceCombinations); |
155 | $services[] = new Service(id: (int) $service->getId(), name: $service->getName(), maxQuantity: $service->getAdditionalData()['maxQuantity'] ?? 1, combinable: $combinable ?? new Combinable()); |
156 | } |
157 | |
158 | return new ServiceList($services); |
159 | } |
160 | |
161 | |
162 | public static function mapRelations(RequestRelationList $relationList): OfficeServiceRelationList |
163 | { |
164 | $relations = []; |
165 | foreach ($relationList as $relation) { |
166 | $relations[] = new OfficeServiceRelation(officeId: (int) $relation->provider->id, serviceId: (int) $relation->request->id, slots: intval($relation->slots)); |
167 | } |
168 | |
169 | return new OfficeServiceRelationList($relations); |
170 | } |
171 | |
172 | public static function scopeToThinnedScope(Scope $scope): ThinnedScope |
173 | { |
174 | if (!$scope || !isset($scope->id)) { |
175 | return new ThinnedScope(); |
176 | } |
177 | |
178 | $thinnedProvider = null; |
179 | try { |
180 | if (isset($scope->provider)) { |
181 | $provider = $scope->provider; |
182 | $contact = $provider->contact ?? null; |
183 | $thinnedProvider = new ThinnedProvider(id: isset($provider->id) ? (int) $provider->id : null, name: $provider->name ?? null, source: $provider->source ?? null, contact: $contact ? self::contactToThinnedContact($contact) : null); |
184 | } |
185 | } catch (\BO\Zmsentities\Exception\ScopeMissingProvider $e) { |
186 | $thinnedProvider = null; |
187 | } |
188 | |
189 | return new ThinnedScope( |
190 | id: (int) ($scope->id ?? 0), |
191 | provider: $thinnedProvider, |
192 | shortName: $scope->shortName ?? null, |
193 | emailFrom: (string) $scope->getEmailFrom() ?? null, |
194 | emailRequired: isset($scope->data['emailRequired']) ? (bool) $scope->data['emailRequired'] : null, |
195 | telephoneActivated: isset($scope->data['telephoneActivated']) ? (bool) $scope->data['telephoneActivated'] : null, |
196 | telephoneRequired: isset($scope->data['telephoneRequired']) ? (bool) $scope->data['telephoneRequired'] : null, |
197 | customTextfieldActivated: isset($scope->data['customTextfieldActivated']) ? (bool) $scope->data['customTextfieldActivated'] : null, |
198 | customTextfieldRequired: isset($scope->data['customTextfieldRequired']) ? (bool) $scope->data['customTextfieldRequired'] : null, |
199 | customTextfieldLabel: $scope->data['customTextfieldLabel'] ?? null, |
200 | captchaActivatedRequired: isset($scope->data['captchaActivatedRequired']) ? (bool) $scope->data['captchaActivatedRequired'] : null, |
201 | displayInfo: $scope->data['displayInfo'] ?? null |
202 | ); |
203 | } |
204 | |
205 | /** |
206 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
207 | * @SuppressWarnings(PHPMD.NPathComplexity) |
208 | * @TODO: Break down process mapping into smaller, focused methods |
209 | */ |
210 | public static function processToThinnedProcess(Process $myProcess): ThinnedProcess |
211 | { |
212 | if (!$myProcess || !isset($myProcess->id)) { |
213 | return new ThinnedProcess(); |
214 | } |
215 | |
216 | $subRequestCounts = []; |
217 | $mainServiceId = null; |
218 | $mainServiceCount = 0; |
219 | $requests = $myProcess->getRequests() ?? []; |
220 | if ($requests) { |
221 | $requests = is_array($requests) ? $requests : iterator_to_array($requests); |
222 | if (count($requests) > 0) { |
223 | $mainServiceId = $requests[0]->id; |
224 | foreach ($requests as $request) { |
225 | if ($request->id === $mainServiceId) { |
226 | $mainServiceCount++; |
227 | } else { |
228 | if (!isset($subRequestCounts[$request->id])) { |
229 | $subRequestCounts[$request->id] = [ |
230 | 'id' => $request->id, |
231 | 'count' => 0, |
232 | ]; |
233 | } |
234 | $subRequestCounts[$request->id]['count']++; |
235 | } |
236 | } |
237 | } |
238 | } |
239 | |
240 | return new ThinnedProcess( |
241 | processId: isset($myProcess->id) ? (int) $myProcess->id : 0, |
242 | timestamp: (isset($myProcess->appointments[0]) && isset($myProcess->appointments[0]->date)) ? strval($myProcess->appointments[0]->date) : null, |
243 | authKey: isset($myProcess->authKey) ? $myProcess->authKey : null, |
244 | familyName: (isset($myProcess->clients[0]) && isset($myProcess->clients[0]->familyName)) ? $myProcess->clients[0]->familyName : null, |
245 | customTextfield: isset($myProcess->customTextfield) ? $myProcess->customTextfield : null, |
246 | email: (isset($myProcess->clients[0]) && isset($myProcess->clients[0]->email)) ? $myProcess->clients[0]->email : null, |
247 | telephone: (isset($myProcess->clients[0]) && isset($myProcess->clients[0]->telephone)) ? $myProcess->clients[0]->telephone : null, |
248 | officeName: (isset($myProcess->scope->contact) && isset($myProcess->scope->contact->name)) ? $myProcess->scope->contact->name : null, |
249 | officeId: (isset($myProcess->scope->provider) && isset($myProcess->scope->provider->id)) ? (int) $myProcess->scope->provider->id : 0, |
250 | scope: isset($myProcess->scope) ? self::scopeToThinnedScope($myProcess->scope) : null, |
251 | subRequestCounts: isset($subRequestCounts) ? array_values($subRequestCounts) : [], |
252 | serviceId: isset($mainServiceId) ? (int) $mainServiceId : 0, |
253 | serviceCount: isset($mainServiceCount) ? $mainServiceCount : 0, |
254 | status: (isset($myProcess->queue) && isset($myProcess->queue->status)) ? $myProcess->queue->status : null |
255 | ); |
256 | } |
257 | |
258 | public static function thinnedProcessToProcess(ThinnedProcess $thinnedProcess): Process |
259 | { |
260 | if (!$thinnedProcess || !isset($thinnedProcess->processId)) { |
261 | return new Process(); |
262 | } |
263 | |
264 | $processEntity = new Process(); |
265 | $processEntity->id = $thinnedProcess->processId; |
266 | $processEntity->authKey = $thinnedProcess->authKey ?? null; |
267 | $processEntity->customTextfield = $thinnedProcess->customTextfield ?? null; |
268 | |
269 | $client = new Client(); |
270 | $client->familyName = $thinnedProcess->familyName ?? null; |
271 | $client->email = $thinnedProcess->email ?? null; |
272 | $client->telephone = $thinnedProcess->telephone ?? null; |
273 | $processEntity->clients = [$client]; |
274 | $appointment = new Appointment(); |
275 | $appointment->date = $thinnedProcess->timestamp ?? null; |
276 | $processEntity->appointments = [$appointment]; |
277 | // Set scope with all required fields |
278 | $scope = new Scope(); |
279 | if ($thinnedProcess->scope) { |
280 | $scope->id = $thinnedProcess->scope->id; |
281 | $scope->source = \App::$source_name; |
282 | |
283 | // Set preferences as array structure |
284 | $scope->preferences = [ |
285 | 'client' => [ |
286 | 'emailFrom' => $thinnedProcess->scope->getEmailFrom() ?? null, |
287 | 'emailRequired' => $thinnedProcess->scope->getEmailRequired() ?? false, |
288 | 'telephoneActivated' => $thinnedProcess->scope->getTelephoneActivated() ?? false, |
289 | 'telephoneRequired' => $thinnedProcess->scope->getTelephoneRequired() ?? false, |
290 | 'customTextfieldActivated' => $thinnedProcess->scope->getCustomTextfieldActivated() ?? false, |
291 | 'customTextfieldRequired' => $thinnedProcess->scope->getCaptchaActivatedRequired() ?? false, |
292 | 'customTextfieldLabel' => $thinnedProcess->scope->getCustomTextfieldLabel() ?? null |
293 | ], |
294 | 'notifications' => [ |
295 | 'enabled' => true |
296 | ] |
297 | ]; |
298 | } |
299 | if (isset($thinnedProcess->officeName)) { |
300 | $scope->contact = new Contact(); |
301 | $scope->contact->name = $thinnedProcess->officeName; |
302 | } |
303 | if (isset($thinnedProcess->officeId)) { |
304 | $scope->provider = new Provider(); |
305 | $scope->provider->id = $thinnedProcess->officeId; |
306 | $scope->provider->source = \App::$source_name; |
307 | } |
308 | $processEntity->scope = $scope; |
309 | if (isset($thinnedProcess->status)) { |
310 | $processEntity->queue = new \stdClass(); |
311 | $processEntity->queue->status = $thinnedProcess->status; |
312 | $processEntity->status = $thinnedProcess->status; |
313 | } |
314 | |
315 | $mainServiceId = $thinnedProcess->serviceId ?? null; |
316 | $mainServiceCount = $thinnedProcess->serviceCount ?? 0; |
317 | $subRequestCounts = $thinnedProcess->subRequestCounts ?? []; |
318 | $requests = []; |
319 | for ($i = 0; $i < $mainServiceCount; $i++) { |
320 | $request = new Request(); |
321 | $request->id = $mainServiceId; |
322 | $request->source = \App::$source_name; |
323 | $requests[] = $request; |
324 | } |
325 | foreach ($subRequestCounts as $subRequest) { |
326 | for ($i = 0; $i < ($subRequest['count'] ?? 0); $i++) { |
327 | $request = new Request(); |
328 | $request->id = $subRequest['id']; |
329 | $request->source = \App::$source_name; |
330 | $requests[] = $request; |
331 | } |
332 | } |
333 | $processEntity->requests = $requests; |
334 | $processEntity->lastChange = time(); |
335 | $processEntity->createIP = ClientIpHelper::getClientIp(); |
336 | $processEntity->createTimestamp = time(); |
337 | return $processEntity; |
338 | } |
339 | |
340 | /** |
341 | * Converts a raw or existing contact object/array into a ThinnedContact model. |
342 | * |
343 | * @param object|array $contact |
344 | * @return ThinnedContact |
345 | */ |
346 | public static function contactToThinnedContact($contact): ThinnedContact |
347 | { |
348 | if (is_array($contact)) { |
349 | return new ThinnedContact(city: $contact['city'] ?? null, country: $contact['country'] ?? null, name: $contact['name'] ?? null, postalCode: $contact['postalCode'] ?? null, region: $contact['region'] ?? null, street: $contact['street'] ?? null, streetNumber: $contact['streetNumber'] ?? null); |
350 | } |
351 | |
352 | return new ThinnedContact(city: $contact->city ?? null, country: $contact->country ?? null, name: $contact->name ?? null, postalCode: $contact->postalCode ?? null, region: $contact->region ?? null, street: $contact->street ?? null, streetNumber: $contact->streetNumber ?? null); |
353 | } |
354 | |
355 | /** |
356 | * Convert a Provider object to a ThinnedProvider. |
357 | * |
358 | * @param Provider $provider |
359 | * @return ThinnedProvider |
360 | */ |
361 | public static function providerToThinnedProvider(Provider $provider): ThinnedProvider |
362 | { |
363 | return new ThinnedProvider(id: isset($provider->id) ? (int) $provider->id : null, name: isset($provider->name) ? $provider->name : null, source: isset($provider->source) ? $provider->source : null, lon: isset($provider->data['geo']['lon']) ? (float) $provider->data['geo']['lon'] : null, lat: isset($provider->data['geo']['lat']) ? (float) $provider->data['geo']['lat'] : null, contact: isset($provider->contact) ? self::contactToThinnedContact($provider->contact) : null); |
364 | } |
365 | } |