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