Lines 91.66% 11 / 12
Functions and Methods 50.00% 1 / 2
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
AppointmentReserveController 91.66% 11 / 12 50.00% 1 / 2 7.03 0.00% 0 / 1
 __construct 100.00% 2 / 2 100.00% 1 / 1 1
 readResponse 90.00% 9 / 10 0.00% 0 / 1 6.04
1<?php
2
3declare(strict_types=1);
4
5namespace BO\Zmscitizenapi\Controllers\Appointment;
6
7use BO\Zmscitizenapi\BaseController;
8use BO\Zmscitizenapi\Controllers\UnpublishedAccessTrait;
9use BO\Zmscitizenapi\Utils\ErrorMessages;
10use BO\Zmscitizenapi\Services\Appointment\AppointmentReserveService;
11use BO\Zmscitizenapi\Services\Core\ValidationService;
12use Psr\Http\Message\RequestInterface;
13use Psr\Http\Message\ResponseInterface;
14
15class AppointmentReserveController extends BaseController
16{
17    use UnpublishedAccessTrait;
18
19    private AppointmentReserveService $service;
20
21    /** @psalm-api */
22    public function __construct()
23    {
24        $this->initializeUnpublishedAccess();
25        $this->service = new AppointmentReserveService();
26    }
27
28    #[\Override]
29    public function readResponse(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
30    {
31        $requestErrors = ValidationService::validateServerPostRequest($request);
32        if (!empty($requestErrors['errors'])) {
33            return $this->createJsonResponse($response, $requestErrors, ErrorMessages::get('invalidRequest')['statusCode']);
34        }
35
36        $result = $this->service->processReservation($request->getParsedBody(), $this->showUnpublished);
37        if (is_array($result) && isset($result['errors'])) {
38            foreach ($result['errors'] as &$error) {
39                if (isset($error['errorCode'])) {
40                    $error = ErrorMessages::get($error['errorCode']);
41                }
42            }
43            return $this->createJsonResponse($response, $result, ErrorMessages::getHighestStatusCode($result['errors']));
44        }
45
46        return $this->createJsonResponse($response, $result->toArray(), 200);
47    }
48}