Passed
Pull Request — master (#73)
by Dmitriy
15:08
created

Response::withResponseFormatter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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