Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.08% covered (success)
96.08%
49 / 51
90.00% covered (success)
90.00%
9 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Render
96.08% covered (success)
96.08%
49 / 51
90.00% covered (success)
90.00%
9 / 10
14
0.00% covered (danger)
0.00%
0 / 1
 withHtml
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 html
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 withXml
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 withJson
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 json
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 xml
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 withLastModified
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 lastModified
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getCachableResponse
83.33% covered (warning)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
4.07
 redirect
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * @package 115Mandant
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Slim;
9
10use App;
11use Fig\Http\Message\StatusCodeInterface;
12use InvalidArgumentException;
13use Psr\Container\ContainerInterface;
14use Psr\Http\Message\RequestInterface;
15use Psr\Http\Message\ResponseInterface;
16use BO\Slim\Response;
17
18class Render
19{
20    /**
21     * @var ContainerInterface $containerInterface
22     *
23     */
24    public static $container = null;
25
26    /**
27     * @var RequestInterface $request;
28     *
29     */
30    public static $request = null;
31
32    /**
33     * @var ResponseInterface $response;
34     *
35     */
36    public static $response = null;
37
38    /**
39     * @return ResponseInterface
40     */
41    public static function withHtml(ResponseInterface $response, $template, $parameters = array(), $status = 200)
42    {
43        Profiler::add("Controller");
44        $response  = $response->withStatus($status);
45        $response  = $response->withHeader('Content-Type', 'text/html; charset=utf-8');
46        App::$templatedefaults['debug'] = App::DEBUG;
47        $parameters = array_merge(App::$templatedefaults, $parameters);
48        $response  = App::$slim->getContainer()->get('view')->render($response, $template, $parameters);
49        Profiler::add("Rendering");
50        return $response ;
51    }
52
53    /**
54     * @return ResponseInterface
55     */
56    public static function html($template, $parameters = array(), $status = 200)
57    {
58        self::$response = self::withHtml(self::$response, $template, $parameters, $status);
59        return self::$response;
60    }
61
62    public static function withXml(ResponseInterface $response, $data, $status = 200)
63    {
64        Profiler::add("Controller");
65        $response = $response->withStatus($status);
66        $response = $response->withHeader('Content-Type', 'application/soap+xml');
67        $response->getBody()->write($data);
68        Profiler::add("Rendering");
69        return $response;
70    }
71
72    public static function withJson(ResponseInterface $response, $data, $status = 200)
73    {
74        Profiler::add("Controller");
75        $response = $response->withStatus($status);
76        $response = $response->withHeader('Content-Type', 'application/json');
77        $response->getBody()->write(json_encode($data, JSON_UNESCAPED_SLASHES));
78        Profiler::add("Rendering");
79        return $response;
80    }
81
82    /**
83     * @return ResponseInterface
84     */
85    public static function json($data, $status = 200)
86    {
87        self::$response = self::withJson(self::$response, $data, $status);
88        return self::$response;
89    }
90
91    /**
92     * @return ResponseInterface
93     */
94    public static function xml($data, $status = 200)
95    {
96        self::$response = self::withXml(self::$response, $data, $status);
97        return self::$response;
98    }
99
100    /**
101     * Add `Last-Modified` header to PSR7 response object
102     *
103     * @param  ResponseInterface $response A PSR7 response object
104     * @param  int|string        $time     A UNIX timestamp or a valid `strtotime()` string
105     *
106     * @return ResponseInterface           A new PSR7 response object with `Last-Modified` header
107     * @throws InvalidArgumentException if the last modified date cannot be parsed
108     */
109    public static function withLastModified(ResponseInterface $response, $date, $expires = '+5 minutes')
110    {
111        return self::getCachableResponse($response, $date, $expires);
112    }
113
114    /**
115     * @param String $date strtotime interpreted
116     * @param String $expires strtotime interpreted
117     *
118     * @return ResponseInterface
119     */
120    public static function lastModified($date, $expires = '+5 minutes')
121    {
122        self::$response = self::withLastModified(self::$response, $date, $expires);
123        return self::$response;
124    }
125
126    /**
127     * @param String $date strtotime interpreted
128     * @param String $expires strtotime interpreted
129     *
130     * @return ResponseInterface
131     */
132    public static function getCachableResponse(
133        ResponseInterface $response,
134        $date,
135        $expires = '+5 minutes'
136    ) {
137
138        if (!$date) {
139            $date = time();
140        } elseif (!is_int($date)) {
141            $date = strtotime($date);
142        }
143
144        $maxAge = strtotime($expires) - time();
145        if (false === strtotime($expires)) {
146            $expires = '+' . $expires . ' seconds';
147            $maxAge = intval($expires);
148        }
149        $response = $response->withAddedHeader('Cache-Control', 'max-age=' . $maxAge);
150        $response = App::$slim->getContainer()->get('cache')->withExpires($response, $expires);
151        $response = App::$slim->getContainer()->get('cache')->withLastModified($response, $date);
152
153        return $response;
154    }
155
156    /**
157     * @param String $route_name
158     * @param array $arguments parameters in the route path
159     * @param array $parameter parameters to append with "?"
160     * @param Int $statuscode see an HTTP reference
161     *
162     * \Psr\Http\Message\ResponseInterface
163     */
164    public static function redirect($route_name, $arguments, $parameter = null, $statuscode = 302)
165    {
166        Profiler::add("Controller");
167
168        $url = App::$slim->urlFor($route_name, $arguments);
169        $url = Helper::proxySanitizeUri($url);
170        $url = preg_replace('#^.*?(https?://)#', '\1', $url); // allow http:// routes
171        if ($parameter) {
172            $url .= '?' . http_build_query($parameter);
173        }
174
175        $response = App::$slim->getResponseFactory()->createResponse($statuscode);
176        $response = App::$slim->getContainer()->get('cache')->denyCache($response);
177        /** @var Response $response */
178        $response = $response->withHeader('Location', (string) $url);
179
180        return $response->withAddedHeader('Cache-Control', 'max-age=0');
181    }
182}