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