GuzzleResponse   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 160
rs 10
c 0
b 0
f 0
wmc 23

19 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A getHeaders() 0 3 1
A getHeaderLine() 0 3 1
A getUnderlyingResponse() 0 3 1
A getReasonPhrase() 0 3 1
A getHeader() 0 3 1
A __construct() 0 3 1
A withHeader() 0 6 1
A getBody() 0 3 1
A withAddedHeader() 0 6 1
A withProtocolVersion() 0 6 1
A getGuzzleVersion() 0 11 3
A getProtocolVersion() 0 3 1
A withBody() 0 5 1
A withoutHeader() 0 6 1
A createResponse() 0 13 3
A getStatusCode() 0 3 1
A hasHeader() 0 3 1
A withStatus() 0 6 1
1
<?php
2
3
namespace Sarahman\OauthTokensClient;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\StreamInterface;
7
use RuntimeException;
8
9
class GuzzleResponse implements ResponseInterface
10
{
11
    /**
12
     * @var ResponseInterface
13
     */
14
    private $response;
15
16
    /**
17
     * @param int $status
18
     * @param array $headers
19
     * @param string|StreamInterface|null $body
20
     * @param string $version
21
     * @param string|null $reason
22
     */
23
    public function __construct($status = 200, array $headers = [], $body = null, $version = '1.1', $reason = null)
24
    {
25
        $this->response = $this->createResponse($status, $headers, $body, $version, $reason);
26
    }
27
28
    /**
29
     * Creates a Guzzle response instance based on available Guzzle version.
30
     *
31
     * @param int $status HTTP status code.
32
     * @param array $headers HTTP headers.
33
     * @param string|StreamInterface|null $body Response body.
34
     * @param string $version HTTP protocol version.
35
     * @param string|null $reason Reason phrase (optional).
36
     * @return ResponseInterface
37
     * @throws RuntimeException If no compatible Guzzle response class is found.
38
     */
39
    private function createResponse($status, array $headers, $body, $version, $reason = null)
40
    {
41
        // Check for Guzzle 7.x/6.x (PSR-7)
42
        if (class_exists('GuzzleHttp\\Psr7\\Response')) {
43
            return new \GuzzleHttp\Psr7\Response($status, $headers, $body, $version, $reason);
44
        }
45
46
        // Check for Guzzle 5.x/4.x (older versions)
47
        if (class_exists('GuzzleHttp\\Message\\Response')) {
48
            return new \GuzzleHttp\Message\Response($status, $headers, $body, ['protocol_version' => $version]);
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Message\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
49
        }
50
51
        throw new RuntimeException('No compatible Guzzle HTTP Response class found');
52
    }
53
54
    /**
55
     * Static factory method for easier instantiation
56
     */
57
    public static function create($status = 200, array $headers = [], $body = null, $version = '1.1', $reason = null)
58
    {
59
        return new static($status, $headers, $body, $version, $reason);
60
    }
61
62
    // Delegate all PSR-7 ResponseInterface methods to the wrapped response
63
64
    public function getStatusCode()
65
    {
66
        return $this->response->getStatusCode();
67
    }
68
69
    public function withStatus($code, $reasonPhrase = '')
70
    {
71
        $clone = clone $this;
72
        $clone->response = $this->response->withStatus($code, $reasonPhrase);
73
74
        return $clone;
75
    }
76
77
    public function getReasonPhrase()
78
    {
79
        return $this->response->getReasonPhrase();
80
    }
81
82
    // PSR-7 MessageInterface methods
83
84
    public function getProtocolVersion()
85
    {
86
        return $this->response->getProtocolVersion();
87
    }
88
89
    public function withProtocolVersion($version)
90
    {
91
        $clone = clone $this;
92
        $clone->response = $this->response->withProtocolVersion($version);
93
94
        return $clone;
95
    }
96
97
    public function getHeaders()
98
    {
99
        return $this->response->getHeaders();
100
    }
101
102
    public function hasHeader($name)
103
    {
104
        return $this->response->hasHeader($name);
105
    }
106
107
    public function getHeader($name)
108
    {
109
        return $this->response->getHeader($name);
110
    }
111
112
    public function getHeaderLine($name)
113
    {
114
        return $this->response->getHeaderLine($name);
115
    }
116
117
    public function withHeader($name, $value)
118
    {
119
        $clone = clone $this;
120
        $clone->response = $this->response->withHeader($name, $value);
121
122
        return $clone;
123
    }
124
125
    public function withAddedHeader($name, $value)
126
    {
127
        $clone = clone $this;
128
        $clone->response = $this->response->withAddedHeader($name, $value);
129
130
        return $clone;
131
    }
132
133
    public function withoutHeader($name)
134
    {
135
        $clone = clone $this;
136
        $clone->response = $this->response->withoutHeader($name);
137
138
        return $clone;
139
    }
140
141
    public function getBody()
142
    {
143
        return $this->response->getBody();
144
    }
145
146
    public function withBody(StreamInterface $body)
147
    {
148
        $clone = clone $this;
149
        $clone->response = $this->response->withBody($body);
150
        return $clone;
151
    }
152
153
    public function getUnderlyingResponse()
154
    {
155
        return $this->response;
156
    }
157
158
    public static function getGuzzleVersion()
159
    {
160
        if (class_exists('GuzzleHttp\\Psr7\\Response')) {
161
            return 'psr7';
162
        }
163
164
        if (class_exists('GuzzleHttp\\Message\\Response')) {
165
            return 'message';
166
        }
167
168
        return 'unknown';
169
    }
170
}
171