Completed
Push — master ( 64e2a2...c94003 )
by Márk
02:03
created

HttpMethodsClient::put()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 3
crap 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
     * @param HttpClient     $httpClient     The client to send requests with.
41
     * @param MessageFactory $messageFactory The message factory to create requests.
42
     */
43 9
    public function __construct(HttpClient $httpClient, MessageFactory $messageFactory)
44
    {
45 9
        $this->httpClient = $httpClient;
46 9
        $this->messageFactory = $messageFactory;
47 9
    }
48
49
    /**
50
     * Sends a GET request.
51
     *
52
     * @param string|UriInterface $uri
53
     * @param array               $headers
54
     *
55
     * @throws Exception
56
     *
57
     * @return ResponseInterface
58
     */
59 1
    public function get($uri, array $headers = [])
60
    {
61 1
        return $this->send('GET', $uri, $headers, null);
62
    }
63
64
    /**
65
     * Sends an HEAD request.
66
     *
67
     * @param string|UriInterface $uri
68
     * @param array               $headers
69
     *
70
     * @throws Exception
71
     *
72
     * @return ResponseInterface
73
     */
74 1
    public function head($uri, array $headers = [])
75
    {
76 1
        return $this->send('HEAD', $uri, $headers, null);
77
    }
78
79
    /**
80
     * Sends a TRACE request.
81
     *
82
     * @param string|UriInterface $uri
83
     * @param array               $headers
84
     *
85
     * @throws Exception
86
     *
87
     * @return ResponseInterface
88
     */
89 1
    public function trace($uri, array $headers = [])
90
    {
91 1
        return $this->send('TRACE', $uri, $headers, null);
92
    }
93
94
    /**
95
     * Sends a POST request.
96
     *
97
     * @param string|UriInterface         $uri
98
     * @param array                       $headers
99
     * @param string|StreamInterface|null $body
100
     *
101
     * @throws Exception
102
     *
103
     * @return ResponseInterface
104
     */
105 1
    public function post($uri, array $headers = [], $body = null)
106
    {
107 1
        return $this->send('POST', $uri, $headers, $body);
108
    }
109
110
    /**
111
     * Sends a PUT request.
112
     *
113
     * @param string|UriInterface         $uri
114
     * @param array                       $headers
115
     * @param string|StreamInterface|null $body
116
     *
117
     * @throws Exception
118
     *
119
     * @return ResponseInterface
120
     */
121 1
    public function put($uri, array $headers = [], $body = null)
122
    {
123 1
        return $this->send('PUT', $uri, $headers, $body);
124
    }
125
126
    /**
127
     * Sends a PATCH request.
128
     *
129
     * @param string|UriInterface         $uri
130
     * @param array                       $headers
131
     * @param string|StreamInterface|null $body
132
     *
133
     * @throws Exception
134
     *
135
     * @return ResponseInterface
136
     */
137 1
    public function patch($uri, array $headers = [], $body = null)
138
    {
139 1
        return $this->send('PATCH', $uri, $headers, $body);
140
    }
141
142
    /**
143
     * Sends a DELETE request.
144
     *
145
     * @param string|UriInterface         $uri
146
     * @param array                       $headers
147
     * @param string|StreamInterface|null $body
148
     *
149
     * @throws Exception
150
     *
151
     * @return ResponseInterface
152
     */
153 1
    public function delete($uri, array $headers = [], $body = null)
154
    {
155 1
        return $this->send('DELETE', $uri, $headers, $body);
156
    }
157
158
    /**
159
     * Sends an OPTIONS request.
160
     *
161
     * @param string|UriInterface         $uri
162
     * @param array                       $headers
163
     * @param string|StreamInterface|null $body
164
     *
165
     * @throws Exception
166
     *
167
     * @return ResponseInterface
168
     */
169 1
    public function options($uri, array $headers = [], $body = null)
170
    {
171 1
        return $this->send('OPTIONS', $uri, $headers, $body);
172
    }
173
174
    /**
175
     * Sends a request with any HTTP method.
176
     *
177
     * @param string                      $method  HTTP method to use.
178
     * @param string|UriInterface         $uri
179
     * @param array                       $headers
180
     * @param string|StreamInterface|null $body
181
     *
182
     * @throws Exception
183
     *
184
     * @return ResponseInterface
185
     */
186
    public function send($method, $uri, array $headers = [], $body = null)
187
    {
188
        return $this->sendRequest($this->messageFactory->createRequest(
189
            $method,
190
            $uri,
191
            $headers,
192
            $body
193
        ));
194
    }
195
196
    /**
197
     * Forward to the underlying HttpClient.
198
     *
199
     * {@inheritdoc}
200
     */
201 1
    public function sendRequest(RequestInterface $request)
202
    {
203 1
        return $this->httpClient->sendRequest($request);
204
    }
205
}
206