Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.67% covered (success)
91.67%
11 / 12
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AppointmentReserveController
91.67% covered (success)
91.67%
11 / 12
50.00% covered (danger)
50.00%
1 / 2
7.03
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
 readResponse
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
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    public function __construct()
22    {
23        $this->initializeUnpublishedAccess();
24        $this->service = new AppointmentReserveService();
25    }
26
27    public function readResponse(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
28    {
29        $requestErrors = ValidationService::validateServerPostRequest($request);
30        if (!empty($requestErrors['errors'])) {
31            return $this->createJsonResponse($response, $requestErrors, ErrorMessages::get('invalidRequest')['statusCode']);
32        }
33
34        $result = $this->service->processReservation($request->getParsedBody(), $this->showUnpublished);
35        if (is_array($result) && isset($result['errors'])) {
36            foreach ($result['errors'] as &$error) {
37                if (isset($error['errorCode'])) {
38                    $error = ErrorMessages::get($error['errorCode']);
39                }
40            }
41            return $this->createJsonResponse($response, $result, ErrorMessages::getHighestStatusCode($result['errors']));
42        }
43
44        return $this->createJsonResponse($response, $result->toArray(), 200);
45    }
46}