Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.29% covered (warning)
89.29%
25 / 28
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
AppointmentConfirmService
89.29% covered (warning)
89.29%
25 / 28
66.67% covered (warning)
66.67%
4 / 6
19.44
0.00% covered (danger)
0.00%
0 / 1
 processConfirm
84.62% covered (warning)
84.62%
11 / 13
0.00% covered (danger)
0.00%
0 / 1
7.18
 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
 confirmProcess
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 sendConfirmationEmail
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\ThinnedProcess;
8use BO\Zmscitizenapi\Services\Core\ValidationService;
9use BO\Zmscitizenapi\Services\Core\ZmsApiFacadeService;
10use BO\Zmscitizenapi\Services\Core\MapperService;
11
12class AppointmentConfirmService
13{
14    public function processConfirm(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        $result = $this->confirmProcess($reservedProcess);
28        if (is_array($result) && !empty($result['errors'])) {
29            return $result;
30        }
31
32        if ($result->status === 'confirmed') {
33            $this->sendConfirmationEmail($result);
34        }
35
36        return $result;
37    }
38
39
40    private function extractClientData(array $body): object
41    {
42        return (object) [
43            'processId' => isset($body['processId']) && is_numeric($body['processId'])
44                ? (int) $body['processId']
45                : null,
46            'authKey' => isset($body['authKey']) && is_string($body['authKey']) && trim($body['authKey']) !== ''
47                ? htmlspecialchars(trim($body['authKey']), ENT_QUOTES, 'UTF-8')
48                : null
49        ];
50    }
51
52    private function validateClientData(object $data): array
53    {
54        return ValidationService::validateGetProcessById($data->processId, $data->authKey);
55    }
56
57    private function getReservedProcess(int $processId, string $authKey): ThinnedProcess|array
58    {
59        return ZmsApiFacadeService::getThinnedProcessById($processId, $authKey);
60    }
61
62    private function confirmProcess(ThinnedProcess $process): ThinnedProcess|array
63    {
64        $processEntity = MapperService::thinnedProcessToProcess($process);
65        $result = ZmsApiFacadeService::confirmAppointment($processEntity);
66        if (is_array($result) && !empty($result['errors'])) {
67            return $result;
68        }
69
70        return MapperService::processToThinnedProcess($result);
71    }
72
73    private function sendConfirmationEmail(ThinnedProcess $process): void
74    {
75        $processEntity = MapperService::thinnedProcessToProcess($process);
76        ZmsApiFacadeService::sendConfirmationEmail($processEntity);
77    }
78}