Lines 100.00% 12 / 12
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
AppointmentCancelController 100.00% 12 / 12 100.00% 2 / 2 7 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 readResponse 100.00% 11 / 11 100.00% 1 / 1 6
1<?php
2
3declare(strict_types=1);
4
5namespace BO\Zmscitizenapi\Controllers\Appointment;
6
7use BO\Zmscitizenapi\BaseController;
8use BO\Zmscitizenapi\Utils\ErrorMessages;
9use BO\Zmscitizenapi\Services\Appointment\AppointmentCancelService;
10use BO\Zmscitizenapi\Services\Core\AuthenticationService;
11use BO\Zmscitizenapi\Services\Core\ValidationService;
12use Psr\Http\Message\RequestInterface;
13use Psr\Http\Message\ResponseInterface;
14
15class AppointmentCancelController extends BaseController
16{
17    private AppointmentCancelService $service;
18    /** @psalm-api */
19    public function __construct()
20    {
21        $this->service = new AppointmentCancelService();
22    }
23
24    #[\Override]
25    public function readResponse(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
26    {
27        $requestErrors = ValidationService::validateServerPostRequest($request);
28        if (!empty($requestErrors['errors'])) {
29            return $this->createJsonResponse($response, $requestErrors, ErrorMessages::get('invalidRequest')['statusCode']);
30        }
31
32        $authenticatedUser = AuthenticationService::getAuthenticatedUser($request);
33        $result = $this->service->processCancel($request->getParsedBody(), $authenticatedUser);
34
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}