Passed
Push — main ( d7b54e...a1a461 )
by Thomas
02:43
created

Response::withStatus()   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 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Conia\Chuck;
6
7
use Conia\Chuck\Exception\RuntimeException;
8
use Conia\Chuck\Http\Factory;
9
use Psr\Http\Message\ResponseInterface as PsrResponse;
10
use Psr\Http\Message\StreamInterface as PsrStream;
11
12
class Response
13
{
14
    use WrapsMessage;
15
16
    protected ?Factory $factory = null;
17
18 61
    public function __construct(
19
        protected PsrResponse $psr7,
20
        PsrStream|Factory|null $streamOrFactory = null,
21
    ) {
22 61
        if ($streamOrFactory) {
23 56
            if ($streamOrFactory instanceof Factory) {
24 54
                $this->factory = $streamOrFactory;
25
            } else {
26 2
                $this->psr7 = $psr7->withBody($streamOrFactory);
27
            }
28
        }
29
    }
30
31 16
    public function psr7(): PsrResponse
32
    {
33 16
        return $this->psr7;
34
    }
35
36 1
    public function setPsr7(PsrResponse $psr7): static
37
    {
38 1
        $this->psr7 = $psr7;
39
40 1
        return $this;
41
    }
42
43 7
    public function status(int $statusCode, ?string $reasonPhrase = null): static
44
    {
45 7
        if (empty($reasonPhrase)) {
46 6
            $this->psr7 = $this->psr7->withStatus($statusCode);
47
        } else {
48 1
            $this->psr7 = $this->psr7->withStatus($statusCode, $reasonPhrase);
49
        }
50
51 7
        return $this;
52
    }
53
54 6
    public function getStatusCode(): int
55
    {
56 6
        return $this->psr7->getStatusCode();
57
    }
58
59 4
    public function getReasonPhrase(): string
60
    {
61 4
        return $this->psr7->getReasonPhrase();
62
    }
63
64 1
    public function protocolVersion(string $protocol): static
65
    {
66 1
        $this->psr7 = $this->psr7->withProtocolVersion($protocol);
67
68 1
        return $this;
69
    }
70
71 9
    public function header(string $name, string $value): static
72
    {
73 9
        $this->psr7 = $this->psr7->withAddedHeader($name, $value);
74
75 9
        return $this;
76
    }
77
78 1
    public function removeHeader(string $name): static
79
    {
80 1
        $this->psr7 = $this->psr7->withoutHeader($name);
81
82 1
        return $this;
83
    }
84
85 7
    public function headers(): array
86
    {
87 7
        return $this->psr7->getHeaders();
88
    }
89
90 13
    public function getHeader(string $name): array
91
    {
92 13
        return $this->psr7->getHeader($name);
93
    }
94
95 3
    public function hasHeader(string $name): bool
96
    {
97 3
        return $this->psr7->hasHeader($name);
98
    }
99
100 8
    public function body(PsrStream|string $body): static
101
    {
102 8
        if ($body instanceof PsrStream) {
103 1
            $this->psr7 = $this->psr7->withBody($body);
104
105 1
            return $this;
106
        }
107
108 7
        if ($this->factory) {
109 6
            $this->psr7 = $this->psr7->withBody($this->factory->stream($body));
110
111 6
            return $this;
112
        }
113
114 1
        throw new RuntimeException('No factory instance set in response object');
115
    }
116
117 33
    public function getBody(): PsrStream
118
    {
119 33
        return $this->psr7->getBody();
120
    }
121
122 3
    public function write(string $content): static
123
    {
124 3
        $this->psr7->getBody()->write($content);
125
126 3
        return $this;
127
    }
128
129 2
    public function redirect(string $url, int $code = 302): static
130
    {
131 2
        $this->header('Location', $url);
132 2
        $this->status($code);
133
134 2
        return $this;
135
    }
136
}
137