Passed
Pull Request — master (#233)
by Dmitriy
02:03
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
/**
11
 * A wrapper around PSR-7 response that is assigned raw data to be formatted later using a formatter.
12
 *
13
 * For example, `['name' => 'Dmitry']` to be formatted to JSON using {@see \Yiisoft\Yii\Web\Formatter\JsonResponseFormatter}
14
 * when {@see WebResponse::getBody()} is called.
15
 */
16
class WebResponse implements ResponseInterface
17
{
18
    private ResponseInterface $response;
19
20
    private $data;
21
22
    private ?StreamInterface $dataStream = null;
23
24
    private ?ResponseFormatterInterface $responseFormatter = null;
25
26
    public function __construct($data, int $code, string $reasonPhrase, ResponseFactoryInterface $responseFactory)
27
    {
28
        $this->response = $responseFactory->createResponse($code, $reasonPhrase);
29
        $this->data = $data;
30
    }
31
32
    public function getBody(): StreamInterface
33
    {
34
        if ($this->dataStream !== null) {
35
            return $this->dataStream;
36
        }
37
38
        if ($this->data === null) {
39
            return $this->dataStream = $this->response->getBody();
40
        }
41
42
        if ($this->responseFormatter !== null) {
43
            $this->response = $this->responseFormatter->format($this);
44
            return $this->dataStream = $this->response->getBody();
45
        }
46
47
        $data = $this->getData();
48
        if (is_string($data)) {
49
            $this->response->getBody()->write($data);
50
            return $this->dataStream = $this->response->getBody();
51
        }
52
53
        throw new \RuntimeException('Data must be a string value.');
54
    }
55
56
    public function getHeader($name): array
57
    {
58
        return $this->response->getHeader($name);
59
    }
60
61
    public function getHeaderLine($name): string
62
    {
63
        return $this->response->getHeaderLine($name);
64
    }
65
66
    public function getHeaders(): array
67
    {
68
        return $this->response->getHeaders();
69
    }
70
71
    public function getProtocolVersion(): string
72
    {
73
        return $this->response->getProtocolVersion();
74
    }
75
76
    public function getReasonPhrase(): string
77
    {
78
        return $this->response->getReasonPhrase();
79
    }
80
81
    public function getStatusCode(): int
82
    {
83
        return $this->response->getStatusCode();
84
    }
85
86
    public function hasHeader($name): bool
87
    {
88
        return $this->response->hasHeader($name);
89
    }
90
91
    public function withAddedHeader($name, $value): WebResponse
92
    {
93
        $response = clone $this;
94
        $response->response = $this->response->withAddedHeader($name, $value);
95
        return $response;
96
    }
97
98
    public function withBody(StreamInterface $body): WebResponse
99
    {
100
        $response = clone $this;
101
        $response->response = $this->response->withBody($body);
102
        $response->dataStream = $body;
103
        return $response;
104
    }
105
106
    public function withHeader($name, $value): WebResponse
107
    {
108
        $response = clone $this;
109
        $response->response = $this->response->withHeader($name, $value);
110
        return $response;
111
    }
112
113
    public function withoutHeader($name): WebResponse
114
    {
115
        $response = clone $this;
116
        $response->response = $this->response->withoutHeader($name);
117
        return $response;
118
    }
119
120
    public function withProtocolVersion($version): WebResponse
121
    {
122
        $response = clone $this;
123
        $response->response = $this->response->withProtocolVersion($version);
124
        return $response;
125
    }
126
127
    public function withStatus($code, $reasonPhrase = ''): WebResponse
128
    {
129
        $response = clone $this;
130
        $response->response = $this->response->withStatus($code, $reasonPhrase);
131
        return $response;
132
    }
133
134
    public function withResponseFormatter(ResponseFormatterInterface $responseFormatter): WebResponse
135
    {
136
        $response = clone $this;
137
        $response->responseFormatter = $responseFormatter;
138
        return $response;
139
    }
140
141
    public function withData($data): WebResponse
142
    {
143
        $response = clone $this;
144
        $response->data = $data;
145
146
        return $response;
147
    }
148
149
    public function hasResponseFormatter(): bool
150
    {
151
        return $this->responseFormatter !== null;
152
    }
153
154
    public function getResponse(): ResponseInterface
155
    {
156
        return $this->response;
157
    }
158
159
    public function getData()
160
    {
161
        if (is_callable($this->data)) {
162
            $this->data = ($this->data)();
163
        }
164
        return is_object($this->data) ? clone $this->data : $this->data;
165
    }
166
}
167