Completed
Push — master ( 955d05...19c28e )
by David
03:37
created

HttpMethodsClient::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.9666
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 3.0416
1
<?php
2
3
namespace Http\Client\Common;
4
5
use Http\Client\Exception;
6
use Http\Client\HttpClient;
7
use Http\Message\RequestFactory;
8
use Psr\Http\Client\ClientInterface;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\StreamInterface;
12
use Psr\Http\Message\UriInterface;
13
14
/**
15
 * Convenience HTTP client that integrates the MessageFactory in order to send
16
 * requests in the following form:.
17
 *
18
 * $client
19
 *     ->get('/foo')
20
 *     ->post('/bar')
21
 * ;
22
 *
23
 * The client also exposes the sendRequest methods of the wrapped HttpClient.
24
 *
25
 * @author Márk Sági-Kazár <[email protected]>
26
 * @author David Buchmann <[email protected]>
27
 */
28
class HttpMethodsClient implements HttpClient
29
{
30
    /**
31
     * @var HttpClient|ClientInterface
32
     */
33
    private $httpClient;
34
35
    /**
36
     * @var RequestFactory
37
     */
38
    private $requestFactory;
39
40
    /**
41
     * @param HttpClient|ClientInterface $httpClient     The client to send requests with
42
     * @param RequestFactory             $requestFactory The message factory to create requests
43
     */
44 9
    public function __construct($httpClient, RequestFactory $requestFactory)
45
    {
46 9
        if (!($httpClient instanceof HttpClient) && !($httpClient instanceof ClientInterface)) {
0 ignored issues
show
Bug introduced by
The class Psr\Http\Client\ClientInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
47
            throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Psr\\Http\\Client\\ClientInterface');
48
        }
49
50 9
        $this->httpClient = $httpClient;
51 9
        $this->requestFactory = $requestFactory;
52 9
    }
53
54
    /**
55
     * Sends a GET request.
56
     *
57
     * @param string|UriInterface $uri
58
     * @param array               $headers
59
     *
60
     * @throws Exception
61
     *
62
     * @return ResponseInterface
63
     */
64 1
    public function get($uri, array $headers = [])
65
    {
66 1
        return $this->send('GET', $uri, $headers, null);
67
    }
68
69
    /**
70
     * Sends an HEAD request.
71
     *
72
     * @param string|UriInterface $uri
73
     * @param array               $headers
74
     *
75
     * @throws Exception
76
     *
77
     * @return ResponseInterface
78
     */
79 1
    public function head($uri, array $headers = [])
80
    {
81 1
        return $this->send('HEAD', $uri, $headers, null);
82
    }
83
84
    /**
85
     * Sends a TRACE request.
86
     *
87
     * @param string|UriInterface $uri
88
     * @param array               $headers
89
     *
90
     * @throws Exception
91
     *
92
     * @return ResponseInterface
93
     */
94 1
    public function trace($uri, array $headers = [])
95
    {
96 1
        return $this->send('TRACE', $uri, $headers, null);
97
    }
98
99
    /**
100
     * Sends a POST request.
101
     *
102
     * @param string|UriInterface         $uri
103
     * @param array                       $headers
104
     * @param string|StreamInterface|null $body
105
     *
106
     * @throws Exception
107
     *
108
     * @return ResponseInterface
109
     */
110 1
    public function post($uri, array $headers = [], $body = null)
111
    {
112 1
        return $this->send('POST', $uri, $headers, $body);
113
    }
114
115
    /**
116
     * Sends a PUT request.
117
     *
118
     * @param string|UriInterface         $uri
119
     * @param array                       $headers
120
     * @param string|StreamInterface|null $body
121
     *
122
     * @throws Exception
123
     *
124
     * @return ResponseInterface
125
     */
126 1
    public function put($uri, array $headers = [], $body = null)
127
    {
128 1
        return $this->send('PUT', $uri, $headers, $body);
129
    }
130
131
    /**
132
     * Sends a PATCH request.
133
     *
134
     * @param string|UriInterface         $uri
135
     * @param array                       $headers
136
     * @param string|StreamInterface|null $body
137
     *
138
     * @throws Exception
139
     *
140
     * @return ResponseInterface
141
     */
142 1
    public function patch($uri, array $headers = [], $body = null)
143
    {
144 1
        return $this->send('PATCH', $uri, $headers, $body);
145
    }
146
147
    /**
148
     * Sends a DELETE request.
149
     *
150
     * @param string|UriInterface         $uri
151
     * @param array                       $headers
152
     * @param string|StreamInterface|null $body
153
     *
154
     * @throws Exception
155
     *
156
     * @return ResponseInterface
157
     */
158 1
    public function delete($uri, array $headers = [], $body = null)
159
    {
160 1
        return $this->send('DELETE', $uri, $headers, $body);
161
    }
162
163
    /**
164
     * Sends an OPTIONS request.
165
     *
166
     * @param string|UriInterface         $uri
167
     * @param array                       $headers
168
     * @param string|StreamInterface|null $body
169
     *
170
     * @throws Exception
171
     *
172
     * @return ResponseInterface
173
     */
174 1
    public function options($uri, array $headers = [], $body = null)
175
    {
176 1
        return $this->send('OPTIONS', $uri, $headers, $body);
177
    }
178
179
    /**
180
     * Sends a request with any HTTP method.
181
     *
182
     * @param string                      $method  HTTP method to use
183
     * @param string|UriInterface         $uri
184
     * @param array                       $headers
185
     * @param string|StreamInterface|null $body
186
     *
187
     * @throws Exception
188
     *
189
     * @return ResponseInterface
190
     */
191
    public function send($method, $uri, array $headers = [], $body = null)
192
    {
193
        return $this->sendRequest($this->requestFactory->createRequest(
194
            $method,
195
            $uri,
196
            $headers,
197
            $body
198
        ));
199
    }
200
201
    /**
202
     * Forward to the underlying HttpClient.
203
     *
204
     * {@inheritdoc}
205
     */
206 1
    public function sendRequest(RequestInterface $request)
207
    {
208 1
        return $this->httpClient->sendRequest($request);
209
    }
210
}
211