Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
90.32% |
28 / 31 |
|
71.43% |
5 / 7 |
CRAP | |
0.00% |
0 / 1 |
AppointmentConfirmService | |
90.32% |
28 / 31 |
|
71.43% |
5 / 7 |
20.36 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
processConfirm | |
86.67% |
13 / 15 |
|
0.00% |
0 / 1 |
7.12 | |||
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 | |||
confirmProcess | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
sendConfirmationEmail | |
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\Captcha\CaptchaService; |
9 | use BO\Zmscitizenapi\Services\Core\ValidationService; |
10 | use BO\Zmscitizenapi\Services\Core\ZmsApiFacadeService; |
11 | use BO\Zmscitizenapi\Services\Core\MapperService; |
12 | |
13 | class AppointmentConfirmService |
14 | { |
15 | private CaptchaService $captchaService; |
16 | |
17 | public function __construct() |
18 | { |
19 | $this->captchaService = new CaptchaService(); |
20 | } |
21 | |
22 | public function processConfirm(array $body): ThinnedProcess|array |
23 | { |
24 | $clientData = $this->extractClientData($body); |
25 | $errors = $this->validateClientData($clientData); |
26 | if (!empty($errors['errors'])) { |
27 | return $errors; |
28 | } |
29 | |
30 | $reservedProcess = $this->getReservedProcess($clientData->processId, $clientData->authKey); |
31 | if (is_array($reservedProcess) && !empty($reservedProcess['errors'])) { |
32 | return $reservedProcess; |
33 | } |
34 | |
35 | // Todo: check if the email template confirmed exists for the scope before submitting and sending |
36 | $result = $this->confirmProcess($reservedProcess); |
37 | if (is_array($result) && !empty($result['errors'])) { |
38 | return $result; |
39 | } |
40 | |
41 | $token = $this->captchaService->generateToken(); |
42 | $result->setCaptchaToken($token); |
43 | |
44 | if ($result->status === 'confirmed') { |
45 | $this->sendConfirmationEmail($result); |
46 | } |
47 | |
48 | return $result; |
49 | } |
50 | |
51 | |
52 | private function extractClientData(array $body): object |
53 | { |
54 | return (object) [ |
55 | 'processId' => isset($body['processId']) && is_numeric($body['processId']) |
56 | ? (int) $body['processId'] |
57 | : null, |
58 | 'authKey' => isset($body['authKey']) && is_string($body['authKey']) && trim($body['authKey']) !== '' |
59 | ? htmlspecialchars(trim($body['authKey']), ENT_QUOTES, 'UTF-8') |
60 | : null |
61 | ]; |
62 | } |
63 | |
64 | private function validateClientData(object $data): array |
65 | { |
66 | return ValidationService::validateGetProcessById($data->processId, $data->authKey); |
67 | } |
68 | |
69 | private function getReservedProcess(int $processId, string $authKey): ThinnedProcess|array |
70 | { |
71 | return ZmsApiFacadeService::getThinnedProcessById($processId, $authKey); |
72 | } |
73 | |
74 | private function confirmProcess(ThinnedProcess $process): ThinnedProcess|array |
75 | { |
76 | $processEntity = MapperService::thinnedProcessToProcess($process); |
77 | $result = ZmsApiFacadeService::confirmAppointment($processEntity); |
78 | if (is_array($result) && !empty($result['errors'])) { |
79 | return $result; |
80 | } |
81 | |
82 | return MapperService::processToThinnedProcess($result); |
83 | } |
84 | |
85 | private function sendConfirmationEmail(ThinnedProcess $process): void |
86 | { |
87 | $processEntity = MapperService::thinnedProcessToProcess($process); |
88 | ZmsApiFacadeService::sendConfirmationEmail($processEntity); |
89 | } |
90 | } |