Completed
Pull Request — master (#131)
by Eric
63:39 queued 61:20
created

HttpAdapterTrait::options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 4
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Ivory Http Adapter package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\HttpAdapter;
13
14
use Ivory\HttpAdapter\Message\InternalRequestInterface;
15
use Psr\Http\Message\RequestInterface;
16
use Psr\Http\Message\ResponseInterface;
17
18
/**
19
 * @author GeLo <[email protected]>
20
 */
21
trait HttpAdapterTrait
22
{
23
    /**
24
     * @param string|object $uri
25
     * @param array         $headers
26
     *
27
     * @throws HttpAdapterException
28
     *
29
     * @return ResponseInterface
30
     */
31 9
    public function get($uri, array $headers = [])
32
    {
33 9
        return $this->send($uri, InternalRequestInterface::METHOD_GET, $headers);
34
    }
35
36
    /**
37
     * @param string|object $uri
38
     * @param array         $headers
39
     *
40
     * @throws HttpAdapterException
41
     *
42
     * @return ResponseInterface
43
     */
44
    public function head($uri, array $headers = [])
45
    {
46
        return $this->send($uri, InternalRequestInterface::METHOD_HEAD, $headers);
47
    }
48
49
    /**
50
     * @param string|object $uri
51
     * @param array         $headers
52
     *
53
     * @throws HttpAdapterException
54
     *
55
     * @return ResponseInterface
56
     */
57
    public function trace($uri, array $headers = [])
58
    {
59
        return $this->send($uri, InternalRequestInterface::METHOD_TRACE, $headers);
60
    }
61
62
    /**
63
     * @param string|object $uri
64
     * @param array         $headers
65
     * @param array|string  $datas
66
     * @param array         $files
67
     *
68
     * @throws HttpAdapterException
69
     *
70
     * @return ResponseInterface
71
     */
72
    public function post($uri, array $headers = [], $datas = [], array $files = [])
73
    {
74
        return $this->send($uri, InternalRequestInterface::METHOD_POST, $headers, $datas, $files);
75
    }
76
77
    /**
78
     * @param string|object $uri
79
     * @param array         $headers
80
     * @param array|string  $datas
81
     * @param array         $files
82
     *
83
     * @throws HttpAdapterException
84
     *
85
     * @return ResponseInterface
86
     */
87
    public function put($uri, array $headers = [], $datas = [], array $files = [])
88
    {
89
        return $this->send($uri, InternalRequestInterface::METHOD_PUT, $headers, $datas, $files);
90
    }
91
92
    /**
93
     * @param string|object $uri
94
     * @param array         $headers
95
     * @param array|string  $datas
96
     * @param array         $files
97
     *
98
     * @throws HttpAdapterException
99
     *
100
     * @return ResponseInterface
101
     */
102
    public function patch($uri, array $headers = [], $datas = [], array $files = [])
103
    {
104
        return $this->send($uri, InternalRequestInterface::METHOD_PATCH, $headers, $datas, $files);
105
    }
106
107
    /**
108
     * @param string|object $uri
109
     * @param array         $headers
110
     * @param array|string  $datas
111
     * @param array         $files
112
     *
113
     * @throws HttpAdapterException
114
     *
115
     * @return ResponseInterface
116
     */
117
    public function delete($uri, array $headers = [], $datas = [], array $files = [])
118
    {
119
        return $this->send($uri, InternalRequestInterface::METHOD_DELETE, $headers, $datas, $files);
120
    }
121
122
    /**
123
     * @param string|object $uri
124
     * @param array         $headers
125
     * @param array|string  $datas
126
     * @param array         $files
127
     *
128
     * @throws HttpAdapterException
129
     *
130
     * @return ResponseInterface
131
     */
132
    public function options($uri, array $headers = [], $datas = [], array $files = [])
133
    {
134
        return $this->send($uri, InternalRequestInterface::METHOD_OPTIONS, $headers, $datas, $files);
135
    }
136
137
    /**
138
     * @param string|object $uri
139
     * @param string        $method
140
     * @param array         $headers
141
     * @param array|string  $datas
142
     * @param array         $files
143
     *
144
     * @throws HttpAdapterException
145
     *
146
     * @return ResponseInterface
147
     */
148 9
    public function send($uri, $method, array $headers = [], $datas = [], array $files = [])
149
    {
150 9
        return $this->sendRequest($this->getConfiguration()->getMessageFactory()->createInternalRequest(
0 ignored issues
show
Bug introduced by
It seems like getConfiguration() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
151 7
            $uri,
152 7
            $method,
153 9
            $this->getConfiguration()->getProtocolVersion(),
0 ignored issues
show
Bug introduced by
It seems like getConfiguration() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
154 7
            $headers,
155 7
            $datas,
156
            $files
157 7
        ));
158
    }
159
160
    /**
161
     * @param RequestInterface $request
162
     *
163
     * @throws HttpAdapterException
164
     *
165
     * @return ResponseInterface
166
     */
167 126
    public function sendRequest(RequestInterface $request)
168
    {
169 126
        if ($request instanceof InternalRequestInterface) {
170 126
            return $this->sendInternalRequest($request);
171
        }
172
173
        $protocolVersion = $this->getConfiguration()->getProtocolVersion();
0 ignored issues
show
Bug introduced by
It seems like getConfiguration() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
174
        $this->getConfiguration()->setProtocolVersion($request->getProtocolVersion());
0 ignored issues
show
Bug introduced by
It seems like getConfiguration() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
175
176
        $response = $this->send(
177
            $request->getUri(),
178
            $request->getMethod(),
179
            $request->getHeaders(),
180
            $request->getBody()
181
        );
182
183
        $this->getConfiguration()->setProtocolVersion($protocolVersion);
0 ignored issues
show
Bug introduced by
It seems like getConfiguration() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
184
185
        return $response;
186
    }
187
188
    /**
189
     * @param array $requests
190
     *
191
     * @throws MultiHttpAdapterException
192
     *
193
     * @return array
194
     */
195 90
    public function sendRequests(array $requests)
196
    {
197 90
        $responses = $exceptions = [];
198
199 90
        foreach ($requests as $index => &$request) {
200 90
            if (is_string($request)) {
201
                $request = [$request];
202
            }
203
204 90
            if (is_array($request)) {
205
                $request = call_user_func_array(
206
                    [$this->getConfiguration()->getMessageFactory(), 'createInternalRequest'],
0 ignored issues
show
Bug introduced by
It seems like getConfiguration() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
207
                    $request
208
                );
209
            }
210
211 90
            if (!$request instanceof RequestInterface) {
212 9
                $exceptions[] = HttpAdapterException::requestIsNotValid($request);
213 9
                unset($requests[$index]);
214 88
            } elseif (!$request instanceof InternalRequestInterface) {
215
                $request = $this->getConfiguration()->getMessageFactory()->createInternalRequest(
0 ignored issues
show
Bug introduced by
It seems like getConfiguration() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
216
                    $request->getUri(),
217
                    $request->getMethod(),
218
                    $request->getProtocolVersion(),
219
                    $request->getHeaders(),
220 20
                    $request->getBody()
221
                );
222
            }
223 70
        }
224
225
        $success = function (ResponseInterface $response) use (&$responses) {
226 63
            $responses[] = $response;
227 90
        };
228
229 90
        $error = function (HttpAdapterException $exception) use (&$exceptions) {
230 27
            $exceptions[] = $exception;
231 90
        };
232
233 90
        $this->sendInternalRequests($requests, $success, $error);
234
235 81
        if (!empty($exceptions)) {
236 36
            throw new MultiHttpAdapterException($exceptions, $responses);
237
        }
238
239 45
        return $responses;
240
    }
241
242
    /**
243
     * @param InternalRequestInterface $internalRequest
244
     *
245
     * @throws HttpAdapterException
246
     *
247
     * @return ResponseInterface
248
     */
249
    abstract protected function sendInternalRequest(InternalRequestInterface $internalRequest);
250
251
    /**
252
     * @param array    $internalRequests
253
     * @param callable $success
254
     * @param callable $error
255
     *
256
     * @throws MultiHttpAdapterException
257
     *
258
     * @return array
259
     */
260 9
    protected function sendInternalRequests(array $internalRequests, $success, $error)
261
    {
262 9
        foreach ($internalRequests as $internalRequest) {
263
            try {
264
                $response = $this->sendInternalRequest($internalRequest);
265
                $response = $response->withParameter('request', $internalRequest);
266
                call_user_func($success, $response);
267
            } catch (HttpAdapterException $e) {
268
                $e->setRequest($internalRequest);
269
                $e->setResponse(isset($response) ? $response : null);
270
                call_user_func($error, $e);
271
            }
272 7
        }
273 9
    }
274
}
275