Completed
Pull Request — master (#23)
by
unknown
04:26
created

HttpMethodsClient   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80.65%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 12
c 2
b 0
f 1
lcom 1
cbo 2
dl 0
loc 198
ccs 25
cts 31
cp 0.8065
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 4 1
A head() 0 4 1
A trace() 0 4 1
A post() 0 4 1
A put() 0 4 1
A patch() 0 4 1
A delete() 0 4 1
A options() 0 4 1
A send() 0 9 1
A sendRequest() 0 6 1
A getLastRequest() 0 4 1
1
<?php
2
3
namespace Http\Client\Common;
4
5
use Http\Client\Exception;
6
use Http\Client\HttpClient;
7
use Http\Message\MessageFactory;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\StreamInterface;
11
use Psr\Http\Message\UriInterface;
12
13
/**
14
 * Convenience HTTP client that integrates the MessageFactory in order to send
15
 * requests in the following form:.
16
 *
17
 * $client
18
 *     ->get('/foo')
19
 *     ->post('/bar')
20
 * ;
21
 *
22
 * The client also exposes the sendRequest methods of the wrapped HttpClient.
23
 *
24
 * @author Márk Sági-Kazár <[email protected]>
25
 * @author David Buchmann <[email protected]>
26
 */
27
class HttpMethodsClient implements HttpClient
28
{
29
    /**
30
     * @var HttpClient
31
     */
32
    private $httpClient;
33
34
    /**
35
     * @var MessageFactory
36
     */
37
    private $messageFactory;
38
39
    /**
40
     * @var RequestInterface
41
     */
42
    private $lastRequest;
43
44
    /**
45
     * @param HttpClient     $httpClient     The client to send requests with.
46
     * @param MessageFactory $messageFactory The message factory to create requests.
47
     */
48 10
    public function __construct(HttpClient $httpClient, MessageFactory $messageFactory)
49
    {
50 10
        $this->httpClient = $httpClient;
51 10
        $this->messageFactory = $messageFactory;
52 10
    }
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->messageFactory->createRequest(
194
            $method,
195
            $uri,
196
            $headers,
197
            $body
198
        ));
199
    }
200
201
    /**
202
     * Forward to the underlying HttpClient.
203
     *
204
     * {@inheritdoc}
205
     */
206 2
    public function sendRequest(RequestInterface $request)
207
    {
208 2
        $this->lastRequest = $request;
209
210 2
        return $this->httpClient->sendRequest($request);
211
    }
212
213
    /**
214
     * Get the last request that was sent via this client.
215
     *
216
     * Note that this will not return requests made directly through the underlying client's sendRequest method.
217
     *
218
     * @return RequestInterface|null
219
     */
220 1
    public function getLastRequest()
221
    {
222 1
        return $this->lastRequest;
223
    }
224
}
225