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