Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.74% |
45 / 47 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
AppointmentUpdateService | |
95.74% |
45 / 47 |
|
66.67% |
4 / 6 |
30 | |
0.00% |
0 / 1 |
processUpdate | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
validateClientData | |
94.12% |
16 / 17 |
|
0.00% |
0 / 1 |
7.01 | |||
extractClientData | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
16 | |||
getReservedProcess | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
updateProcessWithClientData | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
saveProcessUpdate | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace BO\Zmscitizenapi\Services\Appointment; |
6 | |
7 | use BO\Zmscitizenapi\Models\ThinnedProcess; |
8 | use BO\Zmscitizenapi\Services\Core\ValidationService; |
9 | use BO\Zmscitizenapi\Services\Core\ZmsApiFacadeService; |
10 | use BO\Zmscitizenapi\Services\Core\MapperService; |
11 | |
12 | class AppointmentUpdateService |
13 | { |
14 | public function processUpdate(array $body): ThinnedProcess|array |
15 | { |
16 | $clientData = $this->extractClientData($body); |
17 | |
18 | $errors = $this->validateClientData($clientData); |
19 | if (!empty($errors['errors'])) { |
20 | return $errors; |
21 | } |
22 | |
23 | $reservedProcess = $this->getReservedProcess($clientData->processId, $clientData->authKey); |
24 | |
25 | $updatedProcess = $this->updateProcessWithClientData($reservedProcess, $clientData); |
26 | return $this->saveProcessUpdate($updatedProcess); |
27 | } |
28 | |
29 | private function validateClientData(object $data): array |
30 | { |
31 | $authErrors = ValidationService::validateGetProcessById($data->processId, $data->authKey); |
32 | if (is_array($authErrors) && !empty($authErrors['errors'])) { |
33 | return $authErrors; |
34 | } |
35 | |
36 | $reservedProcess = $this->getReservedProcess($data->processId, $data->authKey); |
37 | if (is_array($reservedProcess) && !empty($reservedProcess['errors'])) { |
38 | return $reservedProcess; |
39 | } |
40 | |
41 | $fieldErrors = ValidationService::validateAppointmentUpdateFields( |
42 | $data->familyName, |
43 | $data->email, |
44 | $data->telephone, |
45 | $data->customTextfield, |
46 | $data->customTextfield2, |
47 | $reservedProcess->scope ?? null |
48 | ); |
49 | if (is_array($fieldErrors) && !empty($fieldErrors['errors'])) { |
50 | return $fieldErrors; |
51 | } |
52 | |
53 | return ['errors' => []]; |
54 | } |
55 | |
56 | private function extractClientData(array $body): object |
57 | { |
58 | return (object) [ |
59 | 'processId' => isset($body['processId']) && is_numeric($body['processId']) |
60 | ? (int) $body['processId'] |
61 | : null, |
62 | 'authKey' => isset($body['authKey']) && is_string($body['authKey']) && trim($body['authKey']) !== '' |
63 | ? htmlspecialchars(trim($body['authKey']), ENT_QUOTES, 'UTF-8') |
64 | : null, |
65 | 'familyName' => isset($body['familyName']) && is_string($body['familyName']) ? (string) $body['familyName'] : null, |
66 | 'email' => isset($body['email']) && is_string($body['email']) ? (string) $body['email'] : null, |
67 | 'telephone' => isset($body['telephone']) && is_string($body['telephone']) ? (string) $body['telephone'] : null, |
68 | 'customTextfield' => isset($body['customTextfield']) && is_string($body['customTextfield']) ? (string) $body['customTextfield'] : null, |
69 | 'customTextfield2' => isset($body['customTextfield2']) && is_string($body['customTextfield2']) ? (string) $body['customTextfield2'] : null, |
70 | ]; |
71 | } |
72 | |
73 | private function getReservedProcess(int $processId, string $authKey): ThinnedProcess|array |
74 | { |
75 | return ZmsApiFacadeService::getThinnedProcessById($processId, $authKey); |
76 | } |
77 | |
78 | private function updateProcessWithClientData(ThinnedProcess $process, object $data): ThinnedProcess |
79 | { |
80 | $process->familyName = $data->familyName ?? $process->familyName ?? null; |
81 | $process->email = $data->email ?? $process->email ?? null; |
82 | $process->telephone = $data->telephone ?? $process->telephone ?? null; |
83 | $process->customTextfield = $data->customTextfield ?? $process->customTextfield ?? null; |
84 | $process->customTextfield2 = $data->customTextfield2 ?? $process->customTextfield2 ?? null; |
85 | return $process; |
86 | } |
87 | |
88 | private function saveProcessUpdate(ThinnedProcess $process): ThinnedProcess|array |
89 | { |
90 | $processEntity = MapperService::thinnedProcessToProcess($process); |
91 | $result = ZmsApiFacadeService::updateClientData($processEntity); |
92 | if (is_array($result) && !empty($result['errors'])) { |
93 | return $result; |
94 | } |
95 | |
96 | return MapperService::processToThinnedProcess($result); |
97 | } |
98 | } |