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