Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
15 / 15 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
ResponseFormatter | |
100.00% |
15 / 15 |
|
100.00% |
6 / 6 |
11 | |
100.00% |
1 / 1 |
withStatusCode | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
withJsonHeader | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
withExpiration | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addHeader | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
withBody | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
format | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2020 (original work) Open Assessment Technologies SA |
19 | * |
20 | */ |
21 | |
22 | namespace oat\tao\model\http\formatter; |
23 | |
24 | use JsonSerializable; |
25 | use oat\oatbox\service\ConfigurableService; |
26 | use Psr\Http\Message\ResponseInterface; |
27 | use Psr\Http\Message\StreamInterface; |
28 | |
29 | use function GuzzleHttp\Psr7\stream_for; |
30 | |
31 | class ResponseFormatter extends ConfigurableService |
32 | { |
33 | /** @var int */ |
34 | private $statusCode; |
35 | |
36 | /** @var string[] */ |
37 | private $headers = []; |
38 | |
39 | /** @var StreamInterface */ |
40 | private $body; |
41 | |
42 | public function withStatusCode(int $statusCode): self |
43 | { |
44 | $this->statusCode = $statusCode; |
45 | |
46 | return $this; |
47 | } |
48 | |
49 | public function withJsonHeader(): self |
50 | { |
51 | return $this->addHeader('Content-Type', 'application/json'); |
52 | } |
53 | |
54 | public function withExpiration(int $timestamp): self |
55 | { |
56 | return $this->addHeader('Expires', gmdate('D, d M Y H:i:s \G\M\T', $timestamp)); |
57 | } |
58 | |
59 | public function addHeader(string $name, string $value): self |
60 | { |
61 | $this->headers[$name] = $value; |
62 | |
63 | return $this; |
64 | } |
65 | |
66 | /** |
67 | * @param string|array|JsonSerializable|null|resource|StreamInterface $body |
68 | * |
69 | * @return $this |
70 | */ |
71 | public function withBody($body): self |
72 | { |
73 | $this->body = stream_for(is_array($body) || $body instanceof JsonSerializable ? json_encode($body) : $body); |
74 | |
75 | return $this; |
76 | } |
77 | |
78 | public function format(ResponseInterface $response): ResponseInterface |
79 | { |
80 | if ($this->body) { |
81 | $response = $response->withBody($this->body); |
82 | } |
83 | |
84 | if ($this->statusCode) { |
85 | $response = $response->withStatus($this->statusCode); |
86 | } |
87 | |
88 | foreach ($this->headers as $headerName => $headerValue) { |
89 | $response = $response->withHeader($headerName, $headerValue); |
90 | } |
91 | |
92 | return $response; |
93 | } |
94 | } |