Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.21% covered (warning)
84.21%
32 / 38
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
AvailableAppointmentsListService
84.21% covered (warning)
84.21%
32 / 38
71.43% covered (warning)
71.43%
5 / 7
14.77
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getAvailableAppointmentsList
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
2
 extractClientData
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
5
 isCaptchaRequired
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 validateClientData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAvailableAppointments
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAvailableAppointmentsListByOffice
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace BO\Zmscitizenapi\Services\Availability;
6
7use BO\Zmscitizenapi\Models\AvailableAppointments;
8use BO\Zmscitizenapi\Models\AvailableAppointmentsByOffice;
9use BO\Zmscitizenapi\Services\Captcha\TokenValidationService;
10use BO\Zmscitizenapi\Services\Core\ValidationService;
11use BO\Zmscitizenapi\Services\Core\ZmsApiFacadeService;
12
13class AvailableAppointmentsListService
14{
15    private TokenValidationService $tokenValidator;
16    private ZmsApiFacadeService $zmsApiFacadeService;
17
18    public function __construct()
19    {
20        $this->tokenValidator = new TokenValidationService();
21        $this->zmsApiFacadeService = new ZmsApiFacadeService();
22    }
23
24    public function getAvailableAppointmentsList(array $queryParams): AvailableAppointments|array
25    {
26        $clientData = $this->extractClientData($queryParams);
27        $captchaRequired = $this->isCaptchaRequired($clientData->officeIds);
28        $captchaToken = $queryParams['captchaToken'] ?? null;
29
30        $errors = ValidationService::validateGetAvailableAppointments(
31            $clientData->date,
32            $clientData->officeIds,
33            $clientData->serviceIds,
34            $clientData->serviceCounts,
35            $captchaRequired,
36            $captchaToken,
37            $this->tokenValidator
38        );
39
40        if (!empty($errors['errors'])) {
41            return $errors;
42        }
43
44        return $this->getAvailableAppointments($clientData);
45    }
46
47    private function extractClientData(array $queryParams): object
48    {
49        return (object) [
50            'date' => isset($queryParams['date']) ? (string) $queryParams['date'] : null,
51            'officeIds' => isset($queryParams['officeId'])
52                ? array_map('trim', explode(',', (string) $queryParams['officeId']))
53                : [],
54            'serviceIds' => isset($queryParams['serviceId'])
55                ? array_map('trim', explode(',', (string) $queryParams['serviceId']))
56                : [],
57            'serviceCounts' => isset($queryParams['serviceCount'])
58                ? array_map('trim', explode(',', (string) $queryParams['serviceCount']))
59                : []
60        ];
61    }
62
63    private function isCaptchaRequired(array $officeIds): bool
64    {
65        $officeId = (int)($officeIds[0] ?? 0);
66
67        try {
68            $scope = $this->zmsApiFacadeService->getScopeByOfficeId($officeId);
69            return $scope->captchaActivatedRequired ?? false;
70        } catch (\Throwable $e) {
71            return false;
72        }
73    }
74
75    private function validateClientData(object $data): array
76    {
77        return ValidationService::validateGetAvailableAppointments($data->date, $data->officeIds, $data->serviceIds, $data->serviceCounts);
78    }
79
80    private function getAvailableAppointments(object $data, ?bool $groupByOffice = false): array|AvailableAppointments|AvailableAppointmentsByOffice
81    {
82        return ZmsApiFacadeService::getAvailableAppointments($data->date, $data->officeIds, $data->serviceIds, $data->serviceCounts, $groupByOffice);
83    }
84
85    public function getAvailableAppointmentsListByOffice($queryParams): AvailableAppointmentsByOffice|array
86    {
87        $clientData = $this->extractClientData($queryParams);
88        $errors = $this->validateClientData($clientData);
89        if (!empty($errors['errors'])) {
90            return $errors;
91        }
92
93        return $this->getAvailableAppointments($clientData, true);
94    }
95}