Lines 90.90% 30 / 33
Methods 66.66% 4 / 6
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 processPreconfirm 87.50% 14 / 16 0.00% 0 / 1 7.10
 extractClientData 100.00% 8 / 8 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
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)) {
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}