Passed
Pull Request — master (#73)
by Dmitriy
12:45
created

DeferredResponse   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 25
eloc 45
c 2
b 0
f 0
dl 0
loc 135
rs 10

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A formatResponse() 0 3 1
A getStatusCode() 0 3 1
A withProtocolVersion() 0 4 1
A hasHeader() 0 3 1
A withBody() 0 4 1
A getHeaders() 0 3 1
A getHeader() 0 3 1
A withResponseFormatter() 0 8 2
A withAddedHeader() 0 4 1
A getHeaderLine() 0 3 1
A withStatus() 0 4 1
A getBody() 0 20 5
A getResponse() 0 3 1
A getReasonPhrase() 0 3 1
A getData() 0 3 2
A getProtocolVersion() 0 3 1
A withoutHeader() 0 4 1
A withHeader() 0 4 1
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
        if (is_callable($this->data)) {
36
            $this->data = ($this->data)();
37
        }
38
39
        if ($this->responseFormatter !== null) {
40
            $this->response = $this->formatResponse();
41
            return $this->dataStream = $this->response->getBody();
42
        }
43
44
        if (is_string($this->data)) {
45
            return $this->dataStream = $this->streamFactory->createStream($this->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
        $this->response = $this->response->withAddedHeader($name, $value);
89
        return clone $this;
90
    }
91
92
    public function withBody(StreamInterface $body)
93
    {
94
        $this->dataStream = $body;
95
        return clone $this;
96
    }
97
98
    public function withHeader($name, $value)
99
    {
100
        $this->response = $this->response->withHeader($name, $value);
101
        return clone $this;
102
    }
103
104
    public function withoutHeader($name)
105
    {
106
        $this->response = $this->response->withoutHeader($name);
107
        return clone $this;
108
    }
109
110
    public function withProtocolVersion($version)
111
    {
112
        $this->response = $this->response->withProtocolVersion($version);
113
        return clone $this;
114
    }
115
116
    public function withStatus($code, $reasonPhrase = '')
117
    {
118
        $this->response = $this->response->withStatus($code, $reasonPhrase);
119
        return clone $this;
120
    }
121
122
    public function withResponseFormatter(ResponseFormatterInterface $responseFormatter)
123
    {
124
        if ($this->responseFormatter !== null) {
125
            return $this;
126
        }
127
        $this->responseFormatter = $responseFormatter;
128
129
        return clone $this;
130
    }
131
132
    public function getResponse(): ResponseInterface
133
    {
134
        return $this->response;
135
    }
136
137
    public function getData()
138
    {
139
        return is_object($this->data) ? clone $this->data : $this->data;
140
    }
141
142
    private function formatResponse(): ResponseInterface
143
    {
144
        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

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