Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
22.22% covered (danger)
22.22%
6 / 27
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Controller
22.22% covered (danger)
22.22%
6 / 27
50.00% covered (danger)
50.00%
2 / 4
79.75
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
 __invoke
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
56
 prepareRequest
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 initRequest
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace BO\Slim;
4
5use Psr\Container\ContainerInterface;
6use Psr\Http\Message\RequestInterface;
7use Psr\Http\Message\ResponseInterface;
8
9abstract class Controller
10{
11    /**
12     * @var \Psr\Container\ContainerInterface|null $containerInterface
13     *
14     */
15    protected ?ContainerInterface $containerInterface = null;
16
17    /**
18     * @var \Psr\Http\Message\RequestInterface|null $request
19     *
20     */
21    protected $request = null;
22
23    /**
24     * @var \Psr\Http\Message\ResponseInterface|null $response
25     *
26     */
27    protected $response = null;
28
29    /**
30     * @param \Psr\Container\ContainerInterface $containerInterface
31     *
32     */
33    public function __construct(ContainerInterface $containerInterface)
34    {
35        $this->containerInterface = $containerInterface;
36    }
37
38    public function __invoke(RequestInterface $request, ResponseInterface $response, array $args)
39    {
40        $request = $this->initRequest($request);
41        Render::$request = $request;
42        $this->request = $request;
43        Render::$response = $response;
44        $this->response = $response;
45        Render::$container = $this->containerInterface;
46        $className = get_class($this);
47        ob_start();
48        try {
49            $renderResponse = call_user_func_array([$className, 'render'], $args);
50        } catch (\Exception $exception) {
51            ob_end_clean();
52            throw $exception;
53        }
54        $output = ob_get_clean();
55        if (
56            $output !== false
57            && $output !== ''
58            && $output !== '0'
59            && !$renderResponse instanceof ResponseInterface
60        ) {
61            $renderResponse = Render::$response;
62            $renderResponse->getBody()->write($output);
63        }
64        return $renderResponse instanceof ResponseInterface ? $renderResponse : Render::$response;
65    }
66
67    // init the request with language translation
68    public static function prepareRequest(RequestInterface $request): RequestInterface
69    {
70        /** @psalm-suppress RedundantCondition Module App subclasses may set MULTILANGUAGE to false. */
71        \App::$language = (\App::MULTILANGUAGE) ?
72            new \BO\Slim\Language($request, \App::$supportedLanguages) :
73            new \BO\Slim\Language($request, array_slice(\App::$supportedLanguages, 0));
74        \App::$now = (\App::$now instanceof \DateTimeInterface) ? \App::$now : new \DateTimeImmutable();
75        return $request;
76    }
77
78    public function initRequest(RequestInterface $request): RequestInterface
79    {
80        return self::prepareRequest($request);
81    }
82}