Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.32% covered (success)
90.32%
28 / 31
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
AppointmentPreconfirmService
90.32% covered (success)
90.32%
28 / 31
66.67% covered (warning)
66.67%
4 / 6
20.36
0.00% covered (danger)
0.00%
0 / 1
 processPreconfirm
87.50% covered (warning)
87.50%
14 / 16
0.00% covered (danger)
0.00%
0 / 1
8.12
 extractClientData
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
6
 validateClientData
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getReservedProcess
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 preconfirmProcess
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 sendPreconfirmationEmail
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace BO\Zmscitizenapi\Services\Appointment;
6
7use BO\Zmscitizenapi\Models\AuthenticatedUser;
8use BO\Zmscitizenapi\Models\ThinnedProcess;
9use BO\Zmscitizenapi\Services\Core\ValidationService;
10use BO\Zmscitizenapi\Services\Core\ZmsApiFacadeService;
11use BO\Zmscitizenapi\Services\Core\MapperService;
12
13class 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        $preconfirmErrors = ValidationService::validateAppointmentPreconfirm($reservedProcess);
29        if ($preconfirmErrors['errors'] !== []) {
30            return $preconfirmErrors;
31        }
32
33        // Todo: check if the email template preconfirmed exists for the scope before submitting and sending
34        $result = $this->preconfirmProcess($reservedProcess);
35        if (is_array($result) && !empty($result['errors'])) {
36            return $result;
37        }
38
39        if ($result->status === 'preconfirmed') {
40            $this->sendPreconfirmationEmail($result);
41        }
42
43        return $result;
44    }
45
46    private function extractClientData(array $body): object
47    {
48        return (object) [
49            'processId' => isset($body['processId']) && is_numeric($body['processId'])
50                ? (int) $body['processId']
51                : null,
52            'authKey' => isset($body['authKey']) && is_string($body['authKey']) && trim($body['authKey']) !== ''
53                ? htmlspecialchars(trim($body['authKey']), ENT_QUOTES, 'UTF-8')
54                : null
55        ];
56    }
57
58    private function validateClientData(object $data): array
59    {
60        return ValidationService::validateGetProcessById($data->processId, $data->authKey);
61    }
62
63    private function getReservedProcess(int $processId, ?string $authKey, ?AuthenticatedUser $user): ThinnedProcess|array
64    {
65        return ZmsApiFacadeService::getThinnedProcessById($processId, $authKey, $user);
66    }
67
68    private function preconfirmProcess(ThinnedProcess $process): ThinnedProcess|array
69    {
70        $processEntity = MapperService::thinnedProcessToProcess($process);
71        $result = ZmsApiFacadeService::preconfirmAppointment($processEntity);
72        if (is_array($result) && !empty($result['errors'])) {
73            return $result;
74        }
75
76        return MapperService::processToThinnedProcess($result);
77    }
78
79    private function sendPreconfirmationEmail(ThinnedProcess $process): void
80    {
81        $processEntity = MapperService::thinnedProcessToProcess($process);
82        ZmsApiFacadeService::sendPreconfirmationEmail($processEntity);
83    }
84}