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