Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.12% covered (success)
94.12%
48 / 51
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
AppointmentUpdateService
94.12% covered (success)
94.12%
48 / 51
66.67% covered (warning)
66.67%
4 / 6
34.24
0.00% covered (danger)
0.00%
0 / 1
 processUpdate
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 validateClientData
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
7.01
 extractClientData
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
16
 getReservedProcess
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 updateProcessWithClientData
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 saveProcessUpdate
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
5.58
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;
12use BO\Zmsentities\Helper\ProcessPlainText;
13
14class AppointmentUpdateService
15{
16    public function processUpdate(array $body, ?AuthenticatedUser $authenticatedUser): ThinnedProcess|array
17    {
18        $clientData = $this->extractClientData($body);
19
20        $errors = $this->validateClientData($clientData, $authenticatedUser);
21        if (!empty($errors['errors'])) {
22            return $errors;
23        }
24
25        $reservedProcess = $this->getReservedProcess($clientData->processId, $clientData->authKey, $authenticatedUser);
26
27        $updatedProcess = $this->updateProcessWithClientData($reservedProcess, $clientData);
28        return $this->saveProcessUpdate($updatedProcess, $authenticatedUser);
29    }
30
31    private function validateClientData(object $data, ?AuthenticatedUser $authenticatedUser): array
32    {
33        $authErrors = ValidationService::validateGetProcessById($data->processId, $data->authKey);
34        if (is_array($authErrors) && !empty($authErrors['errors'])) {
35            return $authErrors;
36        }
37
38        $reservedProcess = $this->getReservedProcess($data->processId, $data->authKey, $authenticatedUser);
39        if (is_array($reservedProcess) && !empty($reservedProcess['errors'])) {
40            return $reservedProcess;
41        }
42
43        $fieldErrors = ValidationService::validateAppointmentUpdateFields(
44            $data->familyName,
45            $data->email,
46            $data->telephone,
47            $data->customTextfield,
48            $data->customTextfield2,
49            $reservedProcess->scope ?? null
50        );
51        if (is_array($fieldErrors) && !empty($fieldErrors['errors'])) {
52            return $fieldErrors;
53        }
54
55        return ['errors' => []];
56    }
57
58    private function extractClientData(array $body): object
59    {
60        return (object) [
61            'processId' => isset($body['processId']) && is_numeric($body['processId'])
62                ? (int) $body['processId']
63                : null,
64            'authKey' => isset($body['authKey']) && is_string($body['authKey']) && trim($body['authKey']) !== ''
65                ? htmlspecialchars(trim($body['authKey']), ENT_QUOTES, 'UTF-8')
66                : null,
67            'familyName' => isset($body['familyName']) && is_string($body['familyName']) ? (string) $body['familyName'] : null,
68            'email' => isset($body['email']) && is_string($body['email']) ? (string) $body['email'] : null,
69            'telephone' => isset($body['telephone']) && is_string($body['telephone']) ? (string) $body['telephone'] : null,
70            'customTextfield' => isset($body['customTextfield']) && is_string($body['customTextfield']) ? (string) $body['customTextfield'] : null,
71            'customTextfield2' => isset($body['customTextfield2']) && is_string($body['customTextfield2']) ? (string) $body['customTextfield2'] : null,
72        ];
73    }
74
75    private function getReservedProcess(int $processId, ?string $authKey, ?AuthenticatedUser $user): ThinnedProcess|array
76    {
77        return ZmsApiFacadeService::getThinnedProcessById($processId, $authKey, $user);
78    }
79
80    private function updateProcessWithClientData(ThinnedProcess $process, object $data): ThinnedProcess
81    {
82        $process->familyName = $data->familyName ?? $process->familyName ?? null;
83        $process->email = $data->email ?? $process->email ?? null;
84        $process->telephone = $data->telephone ?? $process->telephone ?? null;
85        if ($data->customTextfield !== null) {
86            $process->customTextfield = ProcessPlainText::normalize($data->customTextfield);
87        }
88        if ($data->customTextfield2 !== null) {
89            $process->customTextfield2 = ProcessPlainText::normalize($data->customTextfield2);
90        }
91        return $process;
92    }
93
94    private function saveProcessUpdate(ThinnedProcess $process, ?AuthenticatedUser $authenticatedUser): ThinnedProcess|array
95    {
96        $processEntity = MapperService::thinnedProcessToProcess($process);
97        if (!is_null($authenticatedUser) && is_null($processEntity->getExternalUserId())) {
98            $processEntity->setExternalUserId($authenticatedUser->getExternalUserId());
99        }
100        $result = ZmsApiFacadeService::updateClientData($processEntity);
101        if (is_array($result) && !empty($result['errors'])) {
102            return $result;
103        }
104
105        return MapperService::processToThinnedProcess($result);
106    }
107}