Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
34 / 34 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
Response | |
100.00% |
34 / 34 |
|
100.00% |
9 / 9 |
19 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
withStatus | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getReasonPhrase | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
withContent | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
withView | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
redirectTo | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
setRenderer | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
with | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
withInput | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Engelsystem\Http; |
6 | |
7 | use Engelsystem\Renderer\Renderer; |
8 | use InvalidArgumentException; |
9 | use Psr\Http\Message\ResponseInterface; |
10 | use Symfony\Component\HttpFoundation\Response as SymfonyResponse; |
11 | use Symfony\Component\HttpFoundation\Session\SessionInterface; |
12 | |
13 | class Response extends SymfonyResponse implements ResponseInterface |
14 | { |
15 | use MessageTrait; |
16 | |
17 | public function __construct( |
18 | string $content = '', |
19 | int $status = 200, |
20 | array $headers = [], |
21 | protected ?Renderer $renderer = null, |
22 | protected ?SessionInterface $session = null |
23 | ) { |
24 | parent::__construct($content, $status, $headers); |
25 | } |
26 | |
27 | /** |
28 | * Return an instance with the specified status code and, optionally, reason phrase. |
29 | * |
30 | * If no reason phrase is specified, implementations MAY choose to default |
31 | * to the RFC 7231 or IANA recommended reason phrase for the response's |
32 | * status code. |
33 | * |
34 | * This method MUST be implemented in such a way as to retain the |
35 | * immutability of the message, and MUST return an instance that has the |
36 | * updated status and reason phrase. |
37 | * |
38 | * @link http://tools.ietf.org/html/rfc7231#section-6 |
39 | * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
40 | * @param int $code The 3-digit integer result code to set. |
41 | * @param string $reasonPhrase The reason phrase to use with the |
42 | * provided status code; if none is provided, implementations MAY |
43 | * use the defaults as suggested in the HTTP specification. |
44 | * @return static |
45 | * @throws InvalidArgumentException For invalid status code arguments. |
46 | */ |
47 | public function withStatus(mixed $code, mixed $reasonPhrase = ''): static |
48 | { |
49 | $new = clone $this; |
50 | $new->setStatusCode($code, !empty($reasonPhrase) ? $reasonPhrase : null); |
51 | |
52 | return $new; |
53 | } |
54 | |
55 | /** |
56 | * Gets the response reason phrase associated with the status code. |
57 | * |
58 | * Because a reason phrase is not a required element in a response |
59 | * status line, the reason phrase value MAY be null. Implementations MAY |
60 | * choose to return the default RFC 7231 recommended reason phrase (or those |
61 | * listed in the IANA HTTP Status Code Registry) for the response's |
62 | * status code. |
63 | * |
64 | * @link http://tools.ietf.org/html/rfc7231#section-6 |
65 | * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
66 | * @return string Reason phrase; must return an empty string if none present. |
67 | */ |
68 | public function getReasonPhrase(): string |
69 | { |
70 | return $this->statusText; |
71 | } |
72 | |
73 | /** |
74 | * Return an instance with the specified content. |
75 | * |
76 | * This method MUST be implemented in such a way as to retain the |
77 | * immutability of the message, and MUST return an instance that has the |
78 | * updated status and reason phrase. |
79 | * |
80 | * @param mixed $content Content that can be cast to string |
81 | * @return static |
82 | */ |
83 | public function withContent(mixed $content): static |
84 | { |
85 | $new = clone $this; |
86 | $new->setContent($content); |
87 | |
88 | return $new; |
89 | } |
90 | |
91 | /** |
92 | * Return an instance with the rendered content. |
93 | * |
94 | * This method retains the immutability of the message and returns |
95 | * an instance with the updated status and headers |
96 | * |
97 | * @param string[]|string[][] $headers |
98 | */ |
99 | public function withView(string $view, array $data = [], int $status = 200, array $headers = []): Response |
100 | { |
101 | if (!$this->renderer instanceof Renderer) { |
102 | throw new InvalidArgumentException('Renderer not defined'); |
103 | } |
104 | |
105 | $new = clone $this; |
106 | $new->setContent($this->renderer->render($view, $data)); |
107 | $new->setStatusCode($status, ($status == $this->getStatusCode() ? $this->statusText : null)); |
108 | |
109 | foreach ($headers as $key => $values) { |
110 | $new = $new->withAddedHeader($key, $values); |
111 | } |
112 | |
113 | return $new; |
114 | } |
115 | |
116 | /** |
117 | * Return an redirect instance |
118 | * |
119 | * This method retains the immutability of the message and returns |
120 | * an instance with the updated status and headers |
121 | */ |
122 | public function redirectTo(string $path, int $status = 302, array $headers = []): Response |
123 | { |
124 | $response = $this->withStatus($status); |
125 | $response = $response->withHeader('location', $path); |
126 | |
127 | foreach ($headers as $name => $value) { |
128 | $response = $response->withAddedHeader($name, $value); |
129 | } |
130 | |
131 | return $response; |
132 | } |
133 | |
134 | /** |
135 | * Set the renderer to use |
136 | */ |
137 | public function setRenderer(Renderer $renderer): void |
138 | { |
139 | $this->renderer = $renderer; |
140 | } |
141 | |
142 | /** |
143 | * Sets a session attribute (which is mutable) |
144 | */ |
145 | public function with(string $key, mixed $value): Response |
146 | { |
147 | if (!$this->session instanceof SessionInterface) { |
148 | throw new InvalidArgumentException('Session not defined'); |
149 | } |
150 | |
151 | $data = $this->session->get($key); |
152 | if (is_array($data) && is_array($value)) { |
153 | $value = array_merge_recursive($data, $value); |
154 | } |
155 | |
156 | $this->session->set($key, $value); |
157 | |
158 | return $this; |
159 | } |
160 | |
161 | /** |
162 | * Sets form data to the mutable session |
163 | */ |
164 | public function withInput(array $input): Response |
165 | { |
166 | if (!$this->session instanceof SessionInterface) { |
167 | throw new InvalidArgumentException('Session not defined'); |
168 | } |
169 | |
170 | foreach ($input as $name => $value) { |
171 | $this->session->set('form-data-' . $name, $value); |
172 | } |
173 | |
174 | return $this; |
175 | } |
176 | } |