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\AuthenticatedUser; |
| 8 | use BO\Zmscitizenapi\Models\ThinnedProcess; |
| 9 | use BO\Zmscitizenapi\Services\Core\ValidationService; |
| 10 | use BO\Zmscitizenapi\Services\Core\ZmsApiFacadeService; |
| 11 | use BO\Zmscitizenapi\Services\Core\MapperService; |
| 12 | |
| 13 | class AppointmentPreconfirmService |
| 14 | { |
| 15 | public function processPreconfirm(array $body, ?AuthenticatedUser $authenticatedUser): ThinnedProcess|array |
| 16 | { |
| 17 | $clientData = $this->extractClientData($body); |
| 18 | $errors = $this->validateClientData($clientData); |
| 19 | if (!empty($errors['errors'])) { |
| 20 | return $errors; |
| 21 | } |
| 22 | |
| 23 | $reservedProcess = $this->getReservedProcess($clientData->processId, $clientData->authKey, $authenticatedUser); |
| 24 | if (is_array($reservedProcess) && !empty($reservedProcess['errors'])) { |
| 25 | return $reservedProcess; |
| 26 | } |
| 27 | |
| 28 | // Todo: check if the email template preconfirmed exists for the scope before submitting and sending |
| 29 | $result = $this->preconfirmProcess($reservedProcess); |
| 30 | if (is_array($result) && !empty($result['errors'])) { |
| 31 | return $result; |
| 32 | } |
| 33 | |
| 34 | if ($result->status === 'preconfirmed') { |
| 35 | $this->sendPreconfirmationEmail($result); |
| 36 | } |
| 37 | |
| 38 | return $result; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | |
| 43 | private function extractClientData(array $body): object |
| 44 | { |
| 45 | return (object) [ |
| 46 | 'processId' => isset($body['processId']) && is_numeric($body['processId']) |
| 47 | ? (int) $body['processId'] |
| 48 | : null, |
| 49 | 'authKey' => isset($body['authKey']) && is_string($body['authKey']) && trim($body['authKey']) !== '' |
| 50 | ? htmlspecialchars(trim($body['authKey']), ENT_QUOTES, 'UTF-8') |
| 51 | : null |
| 52 | ]; |
| 53 | } |
| 54 | |
| 55 | private function validateClientData(object $data): array |
| 56 | { |
| 57 | return ValidationService::validateGetProcessById($data->processId, $data->authKey); |
| 58 | } |
| 59 | |
| 60 | private function getReservedProcess(int $processId, ?string $authKey, ?AuthenticatedUser $user): ThinnedProcess|array |
| 61 | { |
| 62 | return ZmsApiFacadeService::getThinnedProcessById($processId, $authKey, $user); |
| 63 | } |
| 64 | |
| 65 | private function preconfirmProcess(ThinnedProcess $process): ThinnedProcess|array |
| 66 | { |
| 67 | $processEntity = MapperService::thinnedProcessToProcess($process); |
| 68 | $result = ZmsApiFacadeService::preconfirmAppointment($processEntity); |
| 69 | if (is_array($result) && !empty($result['errors'])) { |
| 70 | return $result; |
| 71 | } |
| 72 | |
| 73 | return MapperService::processToThinnedProcess($result); |
| 74 | } |
| 75 | |
| 76 | private function sendPreconfirmationEmail(ThinnedProcess $process): void |
| 77 | { |
| 78 | $processEntity = MapperService::thinnedProcessToProcess($process); |
| 79 | ZmsApiFacadeService::sendPreconfirmationEmail($processEntity); |
| 80 | } |
| 81 | } |