ResponseDecorator   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 190
rs 10
c 0
b 0
f 0
wmc 23

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 12 3
A getHeader() 0 4 1
A getStatusCode() 0 4 1
A withBody() 0 4 1
A withProtocolVersion() 0 4 1
A hasHeader() 0 4 1
A __get() 0 8 2
A __construct() 0 4 1
A withStatus() 0 4 1
A getProtocolVersion() 0 4 1
A __isset() 0 4 1
A withoutHeader() 0 4 1
A withHeader() 0 4 1
A fromBaseResponse() 0 8 2
A getBody() 0 4 1
A getHeaderLine() 0 4 1
A getHeaders() 0 4 1
A withAddedHeader() 0 4 1
A getReasonPhrase() 0 4 1
1
<?php
2
3
namespace Muzzle\Messages;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\StreamInterface;
7
8
trait ResponseDecorator
9
{
10
11
    /**
12
     * @var ResponseInterface
13
     */
14
    protected $response;
15
16
    public function __construct(ResponseInterface $response)
17
    {
18
19
        $this->response = $response;
20
    }
21
22
    /**
23
     * Create a new AssertableResponse from another response.
24
     *
25
     * @param  ResponseInterface $response
26
     * @return self
27
     */
28
    public static function fromBaseResponse($response)
29
    {
30
31
        if ($response instanceof static) {
32
            return $response;
33
        }
34
35
        return new static($response);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getProtocolVersion()
42
    {
43
44
        return $this->response->getProtocolVersion();
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function withProtocolVersion($version)
51
    {
52
53
        return static::fromBaseResponse($this->response->withProtocolVersion($version));
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getHeaders()
60
    {
61
62
        return $this->response->getHeaders();
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function hasHeader($name)
69
    {
70
71
        return $this->response->hasHeader($name);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getHeader($name)
78
    {
79
80
        return $this->response->getHeader($name);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getHeaderLine($name)
87
    {
88
89
        return $this->response->getHeaderLine($name);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function withHeader($name, $value)
96
    {
97
98
        return static::fromBaseResponse($this->response->withHeader($name, $value));
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function withAddedHeader($name, $value)
105
    {
106
107
        return static::fromBaseResponse($this->response->withAddedHeader($name, $value));
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function withoutHeader($name)
114
    {
115
116
        return static::fromBaseResponse($this->response->withoutHeader($name));
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function getBody()
123
    {
124
125
        return $this->response->getBody();
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function withBody(StreamInterface $body)
132
    {
133
134
        return static::fromBaseResponse($this->response->withBody($body));
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function getStatusCode()
141
    {
142
143
        return $this->response->getStatusCode();
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function withStatus($code, $reasonPhrase = '')
150
    {
151
152
        return static::fromBaseResponse($this->response->withStatus($code, $reasonPhrase));
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function getReasonPhrase()
159
    {
160
161
        return $this->response->getReasonPhrase();
162
    }
163
164
    public function __get($property)
165
    {
166
167
        if (property_exists($this->response, $property)) {
168
            return $this->response->{$property};
169
        }
170
171
        return null;
172
    }
173
174
    public function __call($method, $arguments)
175
    {
176
177
        if (method_exists($this->response, $method)) {
178
            $response = $this->response->{$method}(...$arguments);
179
            if (starts_with($method, 'with')) {
180
                return static::fromBaseResponse($response);
181
            }
182
            return $response;
183
        }
184
185
        throw new \BadMethodCallException("The method \"$method\" is not defined.");
186
    }
187
188
    /**
189
     * Proxy isset() checks to the underlying base response.
190
     *
191
     * @param  string $key
192
     * @return mixed
193
     */
194
    public function __isset($key)
195
    {
196
197
        return isset($this->response->{$key});
198
    }
199
}
200