Completed
Push — travis ( f94b47...369e8a )
by Eric
02:24
created

src/HttpAdapterTrait.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
 * Http adapter trait.
20
 *
21
 * @author GeLo <[email protected]>
22
 */
23
trait HttpAdapterTrait
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function get($uri, array $headers = array())
29
    {
30
        return $this->send($uri, InternalRequestInterface::METHOD_GET, $headers);
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function head($uri, array $headers = array())
37
    {
38
        return $this->send($uri, InternalRequestInterface::METHOD_HEAD, $headers);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function trace($uri, array $headers = array())
45
    {
46
        return $this->send($uri, InternalRequestInterface::METHOD_TRACE, $headers);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function post($uri, array $headers = array(), $datas = array(), array $files = array())
53
    {
54
        return $this->send($uri, InternalRequestInterface::METHOD_POST, $headers, $datas, $files);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function put($uri, array $headers = array(), $datas = array(), array $files = array())
61
    {
62
        return $this->send($uri, InternalRequestInterface::METHOD_PUT, $headers, $datas, $files);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function patch($uri, array $headers = array(), $datas = array(), array $files = array())
69
    {
70
        return $this->send($uri, InternalRequestInterface::METHOD_PATCH, $headers, $datas, $files);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function delete($uri, array $headers = array(), $datas = array(), array $files = array())
77
    {
78
        return $this->send($uri, InternalRequestInterface::METHOD_DELETE, $headers, $datas, $files);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function options($uri, array $headers = array(), $datas = array(), array $files = array())
85
    {
86
        return $this->send($uri, InternalRequestInterface::METHOD_OPTIONS, $headers, $datas, $files);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function send($uri, $method, array $headers = array(), $datas = array(), array $files = array())
93
    {
94
        return $this->sendRequest($this->getConfiguration()->getMessageFactory()->createInternalRequest(
95
            $uri,
96
            $method,
97
            $this->getConfiguration()->getProtocolVersion(),
98
            $headers,
99
            $datas,
100
            $files
101
        ));
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function sendRequest(RequestInterface $request)
108
    {
109
        if ($request instanceof InternalRequestInterface) {
110
            return $this->sendInternalRequest($request);
111
        }
112
113
        $protocolVersion = $this->getConfiguration()->getProtocolVersion();
114
        $this->getConfiguration()->setProtocolVersion($request->getProtocolVersion());
115
116
        $response = $this->send(
117
            $request->getUri(),
118
            $request->getMethod(),
119
            $request->getHeaders(),
120
            $request->getBody()
0 ignored issues
show
$request->getBody() is of type object<Psr\Http\Message\StreamInterface>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
        );
122
123
        $this->getConfiguration()->setProtocolVersion($protocolVersion);
124
125
        return $response;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function sendRequests(array $requests)
132
    {
133
        $responses = $exceptions = array();
134
135
        foreach ($requests as $index => &$request) {
136
            if (is_string($request)) {
137
                $request = array($request);
138
            }
139
140
            if (is_array($request)) {
141
                $request = call_user_func_array(
142
                    array($this->getConfiguration()->getMessageFactory(), 'createInternalRequest'),
143
                    $request
144
                );
145
            }
146
147
            if (!$request instanceof RequestInterface) {
148
                $exceptions[] = HttpAdapterException::requestIsNotValid($request);
149
                unset($requests[$index]);
150
            } elseif (!$request instanceof InternalRequestInterface) {
151
                $request = $this->getConfiguration()->getMessageFactory()->createInternalRequest(
152
                    $request->getUri(),
153
                    $request->getMethod(),
154
                    $request->getProtocolVersion(),
155
                    $request->getHeaders(),
156
                    $request->getBody()
157
                );
158
            }
159
        }
160
161
        $success = function (ResponseInterface $response) use (&$responses) {
162
            $responses[] = $response;
163
        };
164
165
        $error = function (HttpAdapterException $exception) use (&$exceptions) {
166
            $exceptions[] = $exception;
167
        };
168
169
        $this->sendInternalRequests($requests, $success, $error);
170
171
        if (!empty($exceptions)) {
172
            throw new MultiHttpAdapterException($exceptions, $responses);
173
        }
174
175
        return $responses;
176
    }
177
178
    /**
179
     * Sends an internal request.
180
     *
181
     * @param \Ivory\HttpAdapter\Message\InternalRequestInterface $internalRequest The internal request.
182
     *
183
     * @throws \Ivory\HttpAdapter\HttpAdapterException If an error occurred.
184
     *
185
     * @return \Ivory\HttpAdapter\Message\ResponseInterface The response.
186
     */
187
    abstract protected function sendInternalRequest(InternalRequestInterface $internalRequest);
188
189
    /**
190
     * Sends internal requests.
191
     *
192
     * @param array    $internalRequests The internal requests.
193
     * @param callable $success          The success callable.
194
     * @param callable $error            The error callable.
195
     *
196
     * @throws \Ivory\HttpAdapter\MultiHttpAdapterException If an error occurred.
197
     *
198
     * @return array The responses.
199
     */
200
    protected function sendInternalRequests(array $internalRequests, $success, $error)
201
    {
202
        foreach ($internalRequests as $internalRequest) {
203
            try {
204
                $response = $this->sendInternalRequest($internalRequest);
205
                $response = $response->withParameter('request', $internalRequest);
206
                call_user_func($success, $response);
207
            } catch (HttpAdapterException $e) {
208
                $e->setRequest($internalRequest);
209
                $e->setResponse(isset($response) ? $response : null);
210
                call_user_func($error, $e);
211
            }
212
        }
213
    }
214
}
215