Passed
Pull Request — master (#73)
by Dmitriy
14:10
created

DeferredResponse::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
        return $this->responseFormatter->/** @scrutinizer ignore-call */ format($this);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
154
    }
155
}
156