Passed
Pull Request — master (#233)
by Alexander
02:12
created

DataResponse::withProtocolVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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