Lines 96.96% 64 / 66
Methods 71.42% 5 / 7
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 2 / 2 100.00% 1 / 1 1
 getAvailableCalendar 93.75% 15 / 16 0.00% 0 / 1 3.00
 extractClientData 100.00% 22 / 22 100.00% 1 / 1 9
 validateClientData 100.00% 13 / 13 100.00% 1 / 1 1
 [BO\Zmscitizenapi\Services\Captcha\CaptchaRequirementTrait] isCaptchaRequiredForOfficeIds 100.00% 1 / 1 100.00% 1 / 1 1
 [BO\Zmscitizenapi\Services\Captcha\CaptchaRequirementTrait] isCaptchaRequiredForOfficeId 100.00% 3 / 3 100.00% 1 / 1 3
 [BO\Zmscitizenapi\Services\Availability\ServiceLocationValidationTrait] validateServiceLocations 88.88% 8 / 9 0.00% 0 / 1 3.01
13class AvailableCalendarService
14{
15    use CaptchaRequirementTrait;
16    use ServiceLocationValidationTrait;
17
18    private TokenValidationService $tokenValidator;
19    private ZmsApiFacadeService $zmsApiFacadeService;
20
21    public function __construct()
22    {
23        $this->tokenValidator = new TokenValidationService();
24        $this->zmsApiFacadeService = new ZmsApiFacadeService();
25    }
26
27    public function getAvailableCalendar(
28        array $queryParams,
29        bool $showUnpublished = false
30    ): AvailableCalendar|array {
31        $clientData = $this->extractClientData($queryParams);
32
33        $errors = $this->validateClientData($clientData);
34        if (!empty($errors['errors'])) {
35            return $errors;
36        }
37
38        $errors = $this->validateServiceLocations($clientData->officeIds, $clientData->serviceIds, $showUnpublished);
39        if ($errors !== null) {
40            return $errors;
41        }
42
43        return ZmsApiFacadeService::getCalendarAvailability(
44            $clientData->officeIds,
45            $clientData->serviceIds,
46            $clientData->serviceCounts,
47            $clientData->startDate,
48            $clientData->endDate,
49            $clientData->slotsStartDate,
50            $clientData->slotsEndDate
51        );
52    }
53
54    private function extractClientData(array $queryParams): object
55    {
56        $queryParams['officeIds'] = isset($queryParams['officeIds']) ? (string) $queryParams['officeIds'] : '';
57        $queryParams['serviceIds'] = isset($queryParams['serviceIds']) ? (string) $queryParams['serviceIds'] : '';
58        $serviceCountsRaw = $queryParams['serviceCounts'] ?? '';
59        $serviceCounts = !empty($serviceCountsRaw)
60            ? array_map('trim', explode(',', (string) $serviceCountsRaw))
61            : [];
62
63        $slotsStartDate = isset($queryParams['slotsStartDate']) && $queryParams['slotsStartDate'] !== ''
64            ? (string) $queryParams['slotsStartDate']
65            : null;
66        $slotsEndDate = isset($queryParams['slotsEndDate']) && $queryParams['slotsEndDate'] !== ''
67            ? (string) $queryParams['slotsEndDate']
68            : null;
69
70        return (object) [
71            'officeIds' => array_map('trim', explode(',', $queryParams['officeIds'])),
72            'serviceIds' => array_map('trim', explode(',', $queryParams['serviceIds'])),
73            'serviceCounts' => $serviceCounts,
74            'startDate' => $queryParams['startDate'] ?? null,
75            'endDate' => $queryParams['endDate'] ?? null,
76            'slotsStartDate' => $slotsStartDate,
77            'slotsEndDate' => $slotsEndDate,
78            'captchaToken' => isset($queryParams['captchaToken']) ? (string) $queryParams['captchaToken'] : null,
79        ];
80    }
81
82    private function validateClientData(object $data): array
83    {
84        $captchaRequired = $this->isCaptchaRequiredForOfficeIds($data->officeIds);
85
86        return ValidationService::validateGetBookableFreeDays(
87            $data->officeIds,
88            $data->serviceIds,
89            $data->startDate,
90            $data->endDate,
91            $data->serviceCounts,
92            $captchaRequired,
93            $data->captchaToken,
94            $this->tokenValidator,
95            $data->slotsStartDate,
96            $data->slotsEndDate
97        );
98    }
99}

From BO\Zmscitizenapi\Services\Captcha\CaptchaRequirementTrait

10trait CaptchaRequirementTrait
11{
12    private function isCaptchaRequiredForOfficeIds(array $officeIds): bool
13    {
14        return $this->zmsApiFacadeService->isCaptchaRequiredForAnyOffice($officeIds);
15    }
16
17    private function isCaptchaRequiredForOfficeId(?int $officeId): bool
18    {
19        if ($officeId === null || $officeId <= 0) {
20            return false;
21        }
22
23        return $this->isCaptchaRequiredForOfficeIds([(string) $officeId]);
24    }
25}

From BO\Zmscitizenapi\Services\Availability\ServiceLocationValidationTrait

9trait ServiceLocationValidationTrait
10{
11    private function validateServiceLocations(array $officeIds, array $serviceIds, bool $showUnpublished = false): ?array
12    {
13        foreach ($officeIds as $officeId) {
14            $errors = ValidationService::validateServiceLocationCombination(
15                (int) $officeId,
16                array_map('intval', $serviceIds),
17                $showUnpublished
18            );
19            if (!empty($errors['errors'])) {
20                return $errors;
21            }
22        }
23
24        return null;
25    }
26}