Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
25.00% covered (danger)
25.00%
6 / 24
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Controller
25.00% covered (danger)
25.00%
6 / 24
50.00% covered (danger)
50.00%
2 / 4
52.19
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 / 17
0.00% covered (danger)
0.00%
0 / 1
30
 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 $containerInterface
13     *
14     */
15    protected $containerInterface = null;
16
17    /**
18     * @var \Psr\Http\Message\RequestInterface $request;
19     *
20     */
21    protected $request = null;
22
23    /**
24     * @var \Psr\Http\Message\ResponseInterface $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 ($output && !$renderResponse instanceof ResponseInterface) {
56            $renderResponse = Render::$response;
57            $renderResponse->getBody()->write($output);
58        }
59        return $renderResponse instanceof ResponseInterface ? $renderResponse : Render::$response;
60    }
61
62    // init the request with language translation
63    public static function prepareRequest(RequestInterface $request)
64    {
65        \App::$language = (\App::MULTILANGUAGE) ?
66            new \BO\Slim\Language($request, \App::$supportedLanguages) :
67            new \BO\Slim\Language($request, array_slice(\App::$supportedLanguages, 0));
68        \App::$now = (! \App::$now) ? new \DateTimeImmutable() : \App::$now;
69        return $request;
70    }
71
72    public function initRequest(RequestInterface $request)
73    {
74        return self::prepareRequest($request);
75    }
76}