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

DeferredResponse::getProtocolVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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
        $this->response = $this->response->withAddedHeader($name, $value);
87
        return clone $this;
88
    }
89
90
    public function withBody(StreamInterface $body)
91
    {
92
        $this->dataStream = $body;
93
        return clone $this;
94
    }
95
96
    public function withHeader($name, $value)
97
    {
98
        $this->response = $this->response->withHeader($name, $value);
99
        return clone $this;
100
    }
101
102
    public function withoutHeader($name)
103
    {
104
        $this->response = $this->response->withoutHeader($name);
105
        return clone $this;
106
    }
107
108
    public function withProtocolVersion($version)
109
    {
110
        $this->response = $this->response->withProtocolVersion($version);
111
        return clone $this;
112
    }
113
114
    public function withStatus($code, $reasonPhrase = '')
115
    {
116
        $this->response = $this->response->withStatus($code, $reasonPhrase);
117
        return clone $this;
118
    }
119
120
    public function withResponseFormatter(ResponseFormatterInterface $responseFormatter)
121
    {
122
        if ($this->responseFormatter !== null) {
123
            return $this;
124
        }
125
        $this->responseFormatter = $responseFormatter;
126
127
        return clone $this;
128
    }
129
130
    public function getResponse(): ResponseInterface
131
    {
132
        return $this->response;
133
    }
134
135
    public function getData()
136
    {
137
        if (is_callable($this->data)) {
138
            $this->data = ($this->data)();
139
        }
140
        return is_object($this->data) ? clone $this->data : $this->data;
141
    }
142
143
    private function formatResponse(): ResponseInterface
144
    {
145
        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

145
        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...
146
    }
147
}
148