Passed
Pull Request — master (#21)
by Sergei
12:41
created

ResponseAccessor::getHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Testing;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\StreamInterface;
9
10
final class ResponseAccessor implements ResponseInterface
11
{
12 15
    public function __construct(private ResponseInterface $response)
13
    {
14 15
    }
15
16 3
    public function getContent(): string
17
    {
18 3
        $stream = $this->response->getBody();
19 3
        $stream->rewind();
20 3
        return $stream->getContents();
21
    }
22
23 1
    public function getContentAsJson(
24
        int $flags = JSON_OBJECT_AS_ARRAY | JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE
25
    ): mixed {
26 1
        return json_decode($this->getContent(), flags: $flags);
27
    }
28
29 2
    public function getProtocolVersion(): string
30
    {
31 2
        return $this->response->getProtocolVersion();
32
    }
33
34
    /**
35
     * @param string $version
36
     */
37 1
    public function withProtocolVersion($version): self
38
    {
39 1
        return $this->withResponse(
40 1
            $this->response->withProtocolVersion($version)
41 1
        );
42
    }
43
44 1
    public function getHeaders(): array
45
    {
46 1
        return $this->response->getHeaders();
47
    }
48
49 1
    public function hasHeader($name): bool
50
    {
51 1
        return $this->response->hasHeader($name);
52
    }
53
54 4
    public function getHeader($name): array
55
    {
56 4
        return $this->response->getHeader($name);
57
    }
58
59 1
    public function getHeaderLine($name): string
60
    {
61 1
        return $this->response->getHeaderLine($name);
62
    }
63
64
    /**
65
     * @param string $name
66
     * @param string|string[] $value
67
     */
68 1
    public function withHeader($name, $value): self
69
    {
70 1
        return $this->withResponse(
71 1
            $this->response->withHeader($name, $value)
72 1
        );
73
    }
74
75
    /**
76
     * @param string $name
77
     * @param string|string[] $value
78
     */
79 2
    public function withAddedHeader($name, $value): self
80
    {
81 2
        return $this->withResponse(
82 2
            $this->response->withAddedHeader($name, $value)
83 2
        );
84
    }
85
86
    /**
87
     * @param string $name
88
     */
89 1
    public function withoutHeader($name): self
90
    {
91 1
        return $this->withResponse(
92 1
            $this->response->withoutHeader($name)
93 1
        );
94
    }
95
96 1
    public function getBody(): StreamInterface
97
    {
98 1
        return $this->response->getBody();
99
    }
100
101 1
    public function withBody(StreamInterface $body): self
102
    {
103 1
        return $this->withResponse(
104 1
            $this->response->withBody($body)
105 1
        );
106
    }
107
108 2
    public function getStatusCode(): int
109
    {
110 2
        return $this->response->getStatusCode();
111
    }
112
113
    /**
114
     * @param int $code
115
     * @param string $reasonPhrase
116
     */
117 1
    public function withStatus($code, $reasonPhrase = ''): self
118
    {
119 1
        return $this->withResponse(
120 1
            $this->response->withStatus($code, $reasonPhrase)
121 1
        );
122
    }
123
124 1
    public function getReasonPhrase(): string
125
    {
126 1
        return $this->response->getReasonPhrase();
127
    }
128
129 6
    public function getResponse(): ResponseInterface
130
    {
131 6
        return $this->response;
132
    }
133
134 6
    private function withResponse(ResponseInterface $response): self
135
    {
136 6
        $new = clone $this;
137 6
        $new->response = $response;
138 6
        return $new;
139
    }
140
}
141