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 | // Todo: check if the email template preconfirmed exists for the scope before submitting and sending |
28 | $result = $this->preconfirmProcess($reservedProcess); |
29 | if (is_array($result) && !empty($result['errors'])) { |
30 | return $result; |
31 | } |
32 | |
33 | if ($result->status === 'preconfirmed') { |
34 | $this->sendPreconfirmationEmail($result); |
35 | } |
36 | |
37 | return $result; |
38 | } |
39 | |
40 | |
41 | |
42 | private function extractClientData(array $body): object |
43 | { |
44 | return (object) [ |
45 | 'processId' => isset($body['processId']) && is_numeric($body['processId']) |
46 | ? (int) $body['processId'] |
47 | : null, |
48 | 'authKey' => isset($body['authKey']) && is_string($body['authKey']) && trim($body['authKey']) !== '' |
49 | ? htmlspecialchars(trim($body['authKey']), ENT_QUOTES, 'UTF-8') |
50 | : null |
51 | ]; |
52 | } |
53 | |
54 | private function validateClientData(object $data): array |
55 | { |
56 | return ValidationService::validateGetProcessById($data->processId, $data->authKey); |
57 | } |
58 | |
59 | private function getReservedProcess(int $processId, string $authKey): ThinnedProcess|array |
60 | { |
61 | return ZmsApiFacadeService::getThinnedProcessById($processId, $authKey); |
62 | } |
63 | |
64 | private function preconfirmProcess(ThinnedProcess $process): ThinnedProcess|array |
65 | { |
66 | $processEntity = MapperService::thinnedProcessToProcess($process); |
67 | $result = ZmsApiFacadeService::preconfirmAppointment($processEntity); |
68 | if (is_array($result) && !empty($result['errors'])) { |
69 | return $result; |
70 | } |
71 | |
72 | return MapperService::processToThinnedProcess($result); |
73 | } |
74 | |
75 | private function sendPreconfirmationEmail(ThinnedProcess $process): void |
76 | { |
77 | $processEntity = MapperService::thinnedProcessToProcess($process); |
78 | ZmsApiFacadeService::sendPreconfirmationEmail($processEntity); |
79 | } |
80 | } |