Lines 100.00% 19 / 19
Functions and Methods 100.00% 5 / 5
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
AppointmentByIdService 100.00% 19 / 19 100.00% 5 / 5 11 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 getAppointmentById 100.00% 8 / 8 100.00% 1 / 1 2
 extractClientData 100.00% 8 / 8 100.00% 1 / 1 6
 validateClientData 100.00% 1 / 1 100.00% 1 / 1 1
 getAppointment 100.00% 1 / 1 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\Captcha\CaptchaService;
10use BO\Zmscitizenapi\Services\Core\ValidationService;
11use BO\Zmscitizenapi\Services\Core\ZmsApiFacadeService;
12
13class AppointmentByIdService
14{
15    private CaptchaService $captchaService;
16
17    public function __construct()
18    {
19        $this->captchaService = new CaptchaService();
20    }
21
22    public function getAppointmentById(array $queryParams, ?AuthenticatedUser $authenticatedUser): ThinnedProcess|array
23    {
24        $clientData = $this->extractClientData($queryParams);
25        $errors = $this->validateClientData($clientData);
26        if (!empty($errors['errors'])) {
27            return $errors;
28        }
29
30        $appointment = $this->getAppointment($clientData->processId, $clientData->authKey, $authenticatedUser);
31
32        $token = $this->captchaService->generateToken();
33        $appointment->setCaptchaToken($token);
34
35        return $appointment;
36    }
37
38    private function extractClientData(array $queryParams): object
39    {
40        return (object) [
41            'processId' => isset($queryParams['processId']) && is_numeric($queryParams['processId'])
42                ? (int) $queryParams['processId']
43                : null,
44            'authKey' => isset($queryParams['authKey']) && is_string($queryParams['authKey']) && trim($queryParams['authKey']) !== ''
45                ? htmlspecialchars(trim($queryParams['authKey']), ENT_QUOTES, 'UTF-8')
46                : null
47        ];
48    }
49
50    private function validateClientData(object $data): array
51    {
52        return ValidationService::validateGetProcessById($data->processId, $data->authKey);
53    }
54
55    private function getAppointment(?int $processId, ?string $authKey, ?AuthenticatedUser $user): ThinnedProcess|array
56    {
57        return ZmsApiFacadeService::getThinnedProcessById($processId, $authKey, $user);
58    }
59}