Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
10 / 11
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AppointmentReserveController
90.91% covered (success)
90.91%
10 / 11
50.00% covered (danger)
50.00%
1 / 2
7.04
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
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\Localization\ErrorMessages;
9use BO\Zmscitizenapi\Services\Appointment\AppointmentReserveService;
10use BO\Zmscitizenapi\Services\Core\ValidationService;
11use Psr\Http\Message\RequestInterface;
12use Psr\Http\Message\ResponseInterface;
13
14class AppointmentReserveController extends BaseController
15{
16    private AppointmentReserveService $service;
17    public function __construct()
18    {
19        $this->service = new AppointmentReserveService();
20    }
21
22    public function readResponse(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
23    {
24        $requestErrors = ValidationService::validateServerPostRequest($request);
25        if (!empty($requestErrors['errors'])) {
26            return $this->createJsonResponse($response, $requestErrors, ErrorMessages::get('invalidRequest', $this->language)['statusCode']);
27        }
28
29        $result = $this->service->processReservation($request->getParsedBody());
30// Handle array errors from validation
31        if (is_array($result) && isset($result['errors'])) {
32// Translate each error message
33            foreach ($result['errors'] as &$error) {
34                if (isset($error['errorCode'])) {
35                    $error = ErrorMessages::get($error['errorCode'], $this->language);
36                }
37            }
38            return $this->createJsonResponse($response, $result, ErrorMessages::getHighestStatusCode($result['errors']));
39        }
40
41        // Handle successful response
42        return $this->createJsonResponse($response, $result->toArray(), 200);
43    }
44}