Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
89.29% |
25 / 28 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
AppointmentPreconfirmService | |
89.29% |
25 / 28 |
|
66.67% |
4 / 6 |
19.44 | |
0.00% |
0 / 1 |
processPreconfirm | |
84.62% |
11 / 13 |
|
0.00% |
0 / 1 |
7.18 | |||
extractClientData | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
6 | |||
validateClientData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getReservedProcess | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
preconfirmProcess | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
sendPreconfirmationEmail | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
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 AppointmentPreconfirmService |
13 | { |
14 | public function processPreconfirm(array $body): ThinnedProcess|array |
15 | { |
16 | $clientData = $this->extractClientData($body); |
17 | $errors = $this->validateClientData($clientData); |
18 | if (!empty($errors['errors'])) { |
19 | return $errors; |
20 | } |
21 | |
22 | $reservedProcess = $this->getReservedProcess($clientData->processId, $clientData->authKey); |
23 | if (is_array($reservedProcess) && !empty($reservedProcess['errors'])) { |
24 | return $reservedProcess; |
25 | } |
26 | |
27 | $result = $this->preconfirmProcess($reservedProcess); |
28 | if (is_array($result) && !empty($result['errors'])) { |
29 | return $result; |
30 | } |
31 | |
32 | if ($result->status === 'preconfirmed') { |
33 | $this->sendPreconfirmationEmail($result); |
34 | } |
35 | |
36 | return $result; |
37 | } |
38 | |
39 | |
40 | |
41 | private function extractClientData(array $body): object |
42 | { |
43 | return (object) [ |
44 | 'processId' => isset($body['processId']) && is_numeric($body['processId']) |
45 | ? (int) $body['processId'] |
46 | : null, |
47 | 'authKey' => isset($body['authKey']) && is_string($body['authKey']) && trim($body['authKey']) !== '' |
48 | ? htmlspecialchars(trim($body['authKey']), ENT_QUOTES, 'UTF-8') |
49 | : null |
50 | ]; |
51 | } |
52 | |
53 | private function validateClientData(object $data): array |
54 | { |
55 | return ValidationService::validateGetProcessById($data->processId, $data->authKey); |
56 | } |
57 | |
58 | private function getReservedProcess(int $processId, string $authKey): ThinnedProcess|array |
59 | { |
60 | return ZmsApiFacadeService::getThinnedProcessById($processId, $authKey); |
61 | } |
62 | |
63 | private function preconfirmProcess(ThinnedProcess $process): ThinnedProcess|array |
64 | { |
65 | $processEntity = MapperService::thinnedProcessToProcess($process); |
66 | $result = ZmsApiFacadeService::preconfirmAppointment($processEntity); |
67 | if (is_array($result) && !empty($result['errors'])) { |
68 | return $result; |
69 | } |
70 | |
71 | return MapperService::processToThinnedProcess($result); |
72 | } |
73 | |
74 | private function sendPreconfirmationEmail(ThinnedProcess $process): void |
75 | { |
76 | $processEntity = MapperService::thinnedProcessToProcess($process); |
77 | ZmsApiFacadeService::sendPreconfirmationEmail($processEntity); |
78 | } |
79 | } |