Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
24 / 30
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
BaseController
80.00% covered (warning)
80.00%
24 / 30
25.00% covered (danger)
25.00%
1 / 4
13.15
0.00% covered (danger)
0.00%
0 / 1
 __invoke
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
3
 getExceptionContext
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 readResponse
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createJsonResponse
76.92% covered (warning)
76.92%
10 / 13
0.00% covered (danger)
0.00%
0 / 1
7.60
1<?php
2
3declare(strict_types=1);
4
5namespace BO\Zmscitizenapi;
6
7use Psr\Http\Message\RequestInterface;
8use Psr\Http\Message\ResponseInterface;
9use BO\Zmscitizenapi\Utils\ErrorMessages;
10
11abstract class BaseController extends \BO\Slim\Controller
12{
13    #[\Override]
14    public function __invoke(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
15    {
16        try {
17            $request = $this->initRequest($request);
18            $noCacheResponse = \BO\Slim\Render::withLastModified($response, time(), '0');
19            return $this->readResponse($request, $noCacheResponse, $args);
20        } catch (\RuntimeException $e) {
21        // Extract error details from the exception message
22            [$errorCode, $errorMessage] = explode(': ', $e->getMessage(), 2);
23            return $this->createJsonResponse($response, [
24                'errors' => [
25                    [
26                        'errorCode' => $errorCode,
27                        'errorMessage' => $errorMessage,
28                        'statusCode' => $e->getCode()
29                    ]
30                ]
31            ], $e->getCode() ?: 500);
32        }
33    }
34
35    protected function getExceptionContext(): string
36    {
37        $className = (new \ReflectionClass($this))->getShortName();
38        return str_replace('Controller', '', $className);
39    }
40
41    /**
42     * Hook method for handling responses in child controllers.
43     * Child classes should override this method to implement their specific response logic.
44     *
45     * @param RequestInterface $request The HTTP request
46     * @param ResponseInterface $response The HTTP response
47     * @param array $args Route parameters
48     * @return ResponseInterface The modified response
49     */
50    public function readResponse(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
51    {
52        return parent::__invoke($request, $response, $args);
53    }
54
55    protected function createJsonResponse(ResponseInterface $response, array $content, int $statusCode): ResponseInterface
56    {
57        if ($statusCode < 100 || $statusCode > 599) {
58            throw new \InvalidArgumentException('Invalid HTTP status code');
59        }
60
61        $response = $response->withStatus($statusCode)
62            ->withHeader('Content-Type', 'application/json; charset=utf-8');
63        if (isset($content['errors'])) {
64            foreach ($content['errors'] as &$error) {
65                if (isset($error['errorCode'])) {
66                    $error = ErrorMessages::get($error['errorCode']);
67                }
68            }
69        }
70
71        try {
72            $json = json_encode($content, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
73        } catch (\JsonException $e) {
74            throw new \RuntimeException('Failed to encode JSON response: ' . $e->getMessage(), 0, $e);
75        }
76
77        $response->getBody()->write($json);
78        return $response;
79    }
80}