Passed
Pull Request — master (#72)
by Dmitriy
29:29 queued 14:31
created

DeferredResponse::getStatusCode()   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 ?DataConverterInterface $dataConverter = 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
        if (is_callable($this->data)) {
36
            $this->data = ($this->data)();
37
        }
38
39
        if ($this->dataConverter !== null) {
40
            return $this->dataStream = $this->convertData();
41
        }
42
43
        if (is_string($this->data)) {
44
            return $this->dataStream = $this->streamFactory->createStream($this->data);
45
        }
46
47
        throw new \RuntimeException('Data must be a string value.');
48
    }
49
50
    public function getHeader($name)
51
    {
52
        return $this->response->getHeader($name);
53
    }
54
55
    public function getHeaderLine($name)
56
    {
57
        return $this->response->getHeaderLine($name);
58
    }
59
60
    public function getHeaders()
61
    {
62
        return $this->response->getHeaders();
63
    }
64
65
    public function getProtocolVersion()
66
    {
67
        return $this->response->getProtocolVersion();
68
    }
69
70
    public function getReasonPhrase()
71
    {
72
        return $this->response->getReasonPhrase();
73
    }
74
75
    public function getStatusCode()
76
    {
77
        return $this->response->getStatusCode();
78
    }
79
80
    public function hasHeader($name)
81
    {
82
        return $this->response->hasHeader($name);
83
    }
84
85
    public function withAddedHeader($name, $value)
86
    {
87
        $this->response = $this->response->withAddedHeader($name, $value);
88
        return clone $this;
89
    }
90
91
    public function withBody(StreamInterface $body)
92
    {
93
        $this->dataStream = $body;
94
        return clone $this;
95
    }
96
97
    public function withHeader($name, $value)
98
    {
99
        $this->response = $this->response->withHeader($name, $value);
100
        return clone $this;
101
    }
102
103
    public function withoutHeader($name)
104
    {
105
        $this->response = $this->response->withoutHeader($name);
106
        return clone $this;
107
    }
108
109
    public function withProtocolVersion($version)
110
    {
111
        $this->response = $this->response->withProtocolVersion($version);
112
        return clone $this;
113
    }
114
115
    public function withStatus($code, $reasonPhrase = '')
116
    {
117
        $this->response = $this->response->withStatus($code, $reasonPhrase);
118
        return clone $this;
119
    }
120
121
    public function withDataConverter(DataConverterInterface $dataConverter)
122
    {
123
        if ($this->dataConverter !== null) {
124
            return $this;
125
        }
126
        $this->dataConverter = $dataConverter;
127
        return clone $this;
128
    }
129
130
    private function convertData(): StreamInterface
131
    {
132
        return $this->dataConverter->convertData($this->data, $this->response);
0 ignored issues
show
Bug introduced by
The method convertData() 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

132
        return $this->dataConverter->/** @scrutinizer ignore-call */ convertData($this->data, $this->response);

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...
133
    }
134
}
135