Passed
Pull Request — master (#233)
by Dmitriy
03:56
created

WebResponse::getBody()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 5
nop 0
dl 0
loc 22
rs 9.5555
c 0
b 0
f 0
1
<?php
2
3
namespace Yiisoft\Yii\Web;
4
5
use Psr\Http\Message\ResponseFactoryInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\StreamInterface;
8
use Yiisoft\Yii\Web\Formatter\ResponseFormatterInterface;
9
10
class WebResponse implements ResponseInterface
11
{
12
    private ResponseInterface $response;
13
14
    private $data;
15
16
    private ?StreamInterface $dataStream = null;
17
18
    private ?ResponseFormatterInterface $responseFormatter = null;
19
20
    public function __construct($data, int $code, ResponseFactoryInterface $responseFactory)
21
    {
22
        $this->response = $responseFactory->createResponse($code);
23
        $this->data = $data;
24
    }
25
26
    public function getBody(): StreamInterface
27
    {
28
        if ($this->dataStream !== null) {
29
            return $this->dataStream;
30
        }
31
32
        if ($this->data === null) {
33
            return $this->dataStream = $this->response->getBody();
34
        }
35
36
        if ($this->responseFormatter !== null) {
37
            $this->response = $this->responseFormatter->format($this);
38
            return $this->dataStream = $this->response->getBody();
39
        }
40
41
        $data = $this->getData();
42
        if (is_string($data)) {
43
            $this->response->getBody()->write($data);
44
            return $this->dataStream = $this->response->getBody();
45
        }
46
47
        throw new \RuntimeException('Data must be a string value.');
48
    }
49
50
    public function getHeader($name): array
51
    {
52
        return $this->response->getHeader($name);
53
    }
54
55
    public function getHeaderLine($name): string
56
    {
57
        return $this->response->getHeaderLine($name);
58
    }
59
60
    public function getHeaders(): array
61
    {
62
        return $this->response->getHeaders();
63
    }
64
65
    public function getProtocolVersion(): string
66
    {
67
        return $this->response->getProtocolVersion();
68
    }
69
70
    public function getReasonPhrase(): string
71
    {
72
        return $this->response->getReasonPhrase();
73
    }
74
75
    public function getStatusCode(): int
76
    {
77
        return $this->response->getStatusCode();
78
    }
79
80
    public function hasHeader($name): bool
81
    {
82
        return $this->response->hasHeader($name);
83
    }
84
85
    public function withAddedHeader($name, $value): WebResponse
86
    {
87
        $response = clone $this;
88
        $response->response = $this->response->withAddedHeader($name, $value);
89
        return $response;
90
    }
91
92
    public function withBody(StreamInterface $body): WebResponse
93
    {
94
        $response = clone $this;
95
        $response->dataStream = $body;
96
        return $response;
97
    }
98
99
    public function withHeader($name, $value): WebResponse
100
    {
101
        $response = clone $this;
102
        $response->response = $this->response->withHeader($name, $value);
103
        return $response;
104
    }
105
106
    public function withoutHeader($name): WebResponse
107
    {
108
        $response = clone $this;
109
        $response->response = $this->response->withoutHeader($name);
110
        return $response;
111
    }
112
113
    public function withProtocolVersion($version): WebResponse
114
    {
115
        $response = clone $this;
116
        $response->response = $this->response->withProtocolVersion($version);
117
        return $response;
118
    }
119
120
    public function withStatus($code, $reasonPhrase = ''): WebResponse
121
    {
122
        $response = clone $this;
123
        $response->response = $this->response->withStatus($code, $reasonPhrase);
124
        return $response;
125
    }
126
127
    public function withResponseFormatter(ResponseFormatterInterface $responseFormatter): WebResponse
128
    {
129
        $response = clone $this;
130
        $response->responseFormatter = $responseFormatter;
131
        return $response;
132
    }
133
134
    public function withData($data): WebResponse
135
    {
136
        $response = clone $this;
137
        $response->data = $data;
138
139
        return $response;
140
    }
141
142
    public function hasResponseFormatter(): bool
143
    {
144
        return $this->responseFormatter !== null;
145
    }
146
147
    public function getResponse(): ResponseInterface
148
    {
149
        return $this->response;
150
    }
151
152
    public function getData()
153
    {
154
        if (is_callable($this->data)) {
155
            $this->data = ($this->data)();
156
        }
157
        return is_object($this->data) ? clone $this->data : $this->data;
158
    }
159
}
160