Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
84.31% |
43 / 51 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| AvailableDaysListService | |
84.31% |
43 / 51 |
|
83.33% |
5 / 6 |
14.76 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getAvailableDaysList | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| extractClientData | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
5 | |||
| validateClientData | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| getAvailableDays | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| getAvailableDaysListByOffice | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace BO\Zmscitizenapi\Services\Availability; |
| 6 | |
| 7 | use BO\Zmscitizenapi\Models\AvailableDays; |
| 8 | use BO\Zmscitizenapi\Models\AvailableDaysByOffice; |
| 9 | use BO\Zmscitizenapi\Services\Captcha\CaptchaRequirementTrait; |
| 10 | use BO\Zmscitizenapi\Services\Captcha\TokenValidationService; |
| 11 | use BO\Zmscitizenapi\Services\Core\ValidationService; |
| 12 | use BO\Zmscitizenapi\Services\Core\ZmsApiFacadeService; |
| 13 | |
| 14 | class AvailableDaysListService |
| 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 getAvailableDaysList(array $queryParams, bool $showUnpublished = false): AvailableDays|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->getAvailableDays($clientData); |
| 42 | } |
| 43 | |
| 44 | private function extractClientData(array $queryParams): object |
| 45 | { |
| 46 | $queryParams['officeId'] = isset($queryParams['officeId']) ? (string) $queryParams['officeId'] : ''; |
| 47 | $queryParams['serviceId'] = isset($queryParams['serviceId']) ? (string) $queryParams['serviceId'] : ''; |
| 48 | $serviceCount = $queryParams['serviceCount'] ?? ''; |
| 49 | $serviceCounts = !empty($serviceCount) |
| 50 | ? array_map('trim', explode(',', (string) $serviceCount)) |
| 51 | : []; |
| 52 | return (object) [ |
| 53 | 'officeIds' => array_map('trim', explode(',', $queryParams['officeId'])), |
| 54 | 'serviceIds' => array_map('trim', explode(',', $queryParams['serviceId'])), |
| 55 | 'serviceCounts' => $serviceCounts, |
| 56 | 'startDate' => $queryParams['startDate'] ?? null, |
| 57 | 'endDate' => $queryParams['endDate'] ?? null, |
| 58 | 'captchaToken' => isset($queryParams['captchaToken']) ? (string) $queryParams['captchaToken'] : null |
| 59 | ]; |
| 60 | } |
| 61 | |
| 62 | private function validateClientData(object $data): array |
| 63 | { |
| 64 | $captchaRequired = $this->isCaptchaRequiredForOfficeIds($data->officeIds); |
| 65 | |
| 66 | return ValidationService::validateGetBookableFreeDays( |
| 67 | $data->officeIds, |
| 68 | $data->serviceIds, |
| 69 | $data->startDate, |
| 70 | $data->endDate, |
| 71 | $data->serviceCounts, |
| 72 | $captchaRequired, |
| 73 | $data->captchaToken, |
| 74 | $this->tokenValidator |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | private function getAvailableDays(object $data, ?bool $groupByOffice = false): AvailableDays|AvailableDaysByOffice|array |
| 79 | { |
| 80 | return ZmsApiFacadeService::getBookableFreeDays( |
| 81 | $data->officeIds, |
| 82 | $data->serviceIds, |
| 83 | $data->serviceCounts, |
| 84 | $data->startDate, |
| 85 | $data->endDate, |
| 86 | $groupByOffice |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | public function getAvailableDaysListByOffice($queryParams, bool $showUnpublished = false) |
| 91 | { |
| 92 | $clientData = $this->extractClientData($queryParams); |
| 93 | $errors = $this->validateClientData($clientData); |
| 94 | if (!empty($errors['errors'])) { |
| 95 | return $errors; |
| 96 | } |
| 97 | |
| 98 | $errors = $this->validateServiceLocations($clientData->officeIds, $clientData->serviceIds, $showUnpublished); |
| 99 | if ($errors !== null) { |
| 100 | return $errors; |
| 101 | } |
| 102 | |
| 103 | return $this->getAvailableDays($clientData, true); |
| 104 | } |
| 105 | } |