Lines
87.50%
42 / 48
Methods
50.00%
3 / 6
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| readResponse | 90.90% | 10 / 11 | 0.00% | 0 / 1 | 5.02 | ||
| [BO\Zmscitizenapi\Controllers\UnpublishedAccessTrait] initializeUnpublishedAccess | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 3 | ||
| [BO\Zmscitizenapi\BaseController] __invoke | 100.00% | 14 / 14 | 100.00% | 1 / 1 | 3 | ||
| [BO\Zmscitizenapi\BaseController] getExceptionContext | 0.00% | 0 / 2 | 0.00% | 0 / 1 | 2 | ||
| [BO\Zmscitizenapi\BaseController] createJsonResponse | 76.92% | 10 / 13 | 0.00% | 0 / 1 | 7.60 | ||
| 15 | class AvailableCalendarController extends BaseController | |
| 16 | { | |
| 17 | use UnpublishedAccessTrait; | |
| 18 | ||
| 19 | private AvailableCalendarService $service; | |
| 20 | ||
| 21 | /** @psalm-api */ | |
| 22 | public function __construct() | |
| 23 | { | |
| 24 | $this->initializeUnpublishedAccess(); | |
| 25 | $this->service = new AvailableCalendarService(); | |
| 26 | } | |
| 27 | ||
| 28 | #[\Override] | |
| 29 | public function readResponse(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface | |
| 30 | { | |
| 31 | $requestErrors = ValidationService::validateServerGetRequest($request); | |
| 32 | if (!empty($requestErrors['errors'])) { | |
| 33 | return $this->createJsonResponse($response, $requestErrors, ErrorMessages::get('invalidRequest')['statusCode']); | |
| 34 | } | |
| 35 | ||
| 36 | $queryParams = $request->getQueryParams(); | |
| 37 | $result = $this->service->getAvailableCalendar($queryParams, $this->showUnpublished); | |
| 38 | ||
| 39 | $isError = is_array($result) && isset($result['errors']); | |
| 40 | return $this->createJsonResponse( | |
| 41 | $response, | |
| 42 | $isError ? $result : $result->toArray(), | |
| 43 | $isError ? ErrorMessages::getHighestStatusCode($result['errors']) : 200 | |
| 44 | ); | |
| 45 | } | |
| 46 | } |
From BO\Zmscitizenapi\Controllers\UnpublishedAccessTrait
| 7 | trait UnpublishedAccessTrait | |
| 8 | { | |
| 9 | private bool $showUnpublished; | |
| 10 | private function initializeUnpublishedAccess(): void | |
| 11 | { | |
| 12 | if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) { | |
| 13 | $domain = $_SERVER['HTTP_X_FORWARDED_HOST']; | |
| 14 | } else { | |
| 15 | $domain = $_SERVER['HTTP_HOST'] ?? ''; | |
| 16 | } | |
| 17 | ||
| 18 | $showUnpublishedOnDomain = App::getAccessUnpublishedOnDomain(); | |
| 19 | $this->showUnpublished = !empty($showUnpublishedOnDomain) | |
| 20 | && strpos($domain, $showUnpublishedOnDomain) !== false; | |
| 21 | } | |
| 22 | } |
Inherited from BO\Zmscitizenapi\BaseController
| 14 | public function __invoke(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface | |
| 15 | { | |
| 16 | try { | |
| 17 | $request = $this->initRequest($request); | |
| 18 | $noCacheResponse = \BO\Slim\Render::withLastModified($response, time(), '0'); | |
| 19 | return $this->readResponse($request, $noCacheResponse, $args); | |
| 20 | } catch (\RuntimeException $e) { | |
| 21 | // Extract error details from the exception message | |
| 22 | [$errorCode, $errorMessage] = explode(': ', $e->getMessage(), 2); | |
| 23 | return $this->createJsonResponse($response, [ | |
| 24 | 'errors' => [ | |
| 25 | [ | |
| 26 | 'errorCode' => $errorCode, | |
| 27 | 'errorMessage' => $errorMessage, | |
| 28 | 'statusCode' => $e->getCode() | |
| 29 | ] | |
| 30 | ] | |
| 31 | ], $e->getCode() ?: 500); | |
| 32 | } | |
| 33 | } |
| 36 | protected function getExceptionContext(): string | |
| 37 | { | |
| 38 | $className = (new \ReflectionClass($this))->getShortName(); | |
| 39 | return str_replace('Controller', '', $className); | |
| 40 | } |
| 56 | protected function createJsonResponse(ResponseInterface $response, array $content, int $statusCode): ResponseInterface | |
| 57 | { | |
| 58 | if ($statusCode < 100 || $statusCode > 599) { | |
| 59 | throw new \InvalidArgumentException('Invalid HTTP status code'); | |
| 60 | } | |
| 61 | ||
| 62 | $response = $response->withStatus($statusCode) | |
| 63 | ->withHeader('Content-Type', 'application/json; charset=utf-8'); | |
| 64 | if (isset($content['errors'])) { | |
| 65 | foreach ($content['errors'] as &$error) { | |
| 66 | if (isset($error['errorCode'])) { | |
| 67 | $error = ErrorMessages::get($error['errorCode']); | |
| 68 | } | |
| 69 | } | |
| 70 | } | |
| 71 | ||
| 72 | try { | |
| 73 | $json = json_encode($content, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); | |
| 74 | } catch (\JsonException $e) { | |
| 75 | throw new \RuntimeException('Failed to encode JSON response: ' . $e->getMessage(), 0, $e); | |
| 76 | } | |
| 77 | ||
| 78 | $response->getBody()->write($json); | |
| 79 | return $response; | |
| 80 | } |