Completed
Pull Request — master (#129)
by Eric
319:50 queued 317:35
created

Guzzle3HttpAdapter::sendInternalRequest()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 21
cts 21
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 18
nc 2
nop 1
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 Guzzle\Common\Event;
15
use Guzzle\Http\Client;
16
use Guzzle\Http\ClientInterface;
17
use Guzzle\Http\Exception\RequestException;
18
use Ivory\HttpAdapter\Message\InternalRequestInterface;
19
use Ivory\HttpAdapter\Normalizer\BodyNormalizer;
20
21
/**
22
 * Guzzle 3 http adapter.
23
 *
24
 * @author GeLo <[email protected]>
25
 */
26
class Guzzle3HttpAdapter extends AbstractCurlHttpAdapter
27
{
28
    /** @var \Guzzle\Http\ClientInterface */
29
    private $client;
30
31
    /**
32
     * Creates a guzzle 3 http adapter.
33
     *
34
     * @param \Guzzle\Http\ClientInterface|null              $client        The guzzle 3 client.
35
     * @param \Ivory\HttpAdapter\ConfigurationInterface|null $configuration The configuration.
36
     */
37 705
    public function __construct(ClientInterface $client = null, ConfigurationInterface $configuration = null)
38
    {
39 705
        parent::__construct($configuration);
40
41 704
        $this->client = $client ?: new Client();
42 704
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 49
    public function getName()
48
    {
49 49
        return 'guzzle3';
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 692
    protected function sendInternalRequest(InternalRequestInterface $internalRequest)
56
    {
57
        try {
58 656
            $response = $this->createRequest($internalRequest)->send();
59 498
        } catch (RequestException $e) {
60 24
            throw HttpAdapterException::cannotFetchUri(
61 24
                $e->getRequest()->getUrl(),
0 ignored issues
show
Bug introduced by
It seems like $e->getRequest()->getUrl() targeting Guzzle\Http\Message\RequestInterface::getUrl() can also be of type object<Guzzle\Http\Url>; however, Ivory\HttpAdapter\HttpAd...ption::cannotFetchUri() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
62 24
                $this->getName(),
63 24
                $e->getMessage()
64 18
            );
65
        }
66
67 632
        return $this->getConfiguration()->getMessageFactory()->createResponse(
68 632
            $response->getStatusCode(),
69 632
            $response->getProtocolVersion(),
70 632
            $response->getHeaders()->toArray(),
71 632
            BodyNormalizer::normalize(
72
                function () use ($response) {
73 584
                    $resource = $response->getBody()->getStream();
74 584
                    $response->getBody()->detachStream();
75
76 584
                    return $resource;
77 632
                },
78 632
                $internalRequest->getMethod()
79 510
            )
80 474
        );
81 528
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 16
    protected function sendInternalRequests(array $internalRequests, $success, $error)
87
    {
88 16
        $requests = array();
89 16
        foreach ($internalRequests as $internalRequest) {
90 16
            $requests[] = $this->createRequest($internalRequest, $success, $error);
91 12
        }
92
93
        try {
94 16
            $this->client->send($requests);
95 14
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
96
        }
97 16
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 80
    protected function createFile($file)
103
    {
104 80
        return '@'.$file;
105
    }
106
107
    /**
108
     * Creates a request.
109
     *
110
     * @param \Ivory\HttpAdapter\Message\InternalRequestInterface $internalRequest The internal request.
111
     * @param callable|null                                       $success         The success callable.
112
     * @param callable|null                                       $error           The error callable.
113
     *
114
     * @return \Ivory\HttpAdapter\Message\RequestInterface The request.
115
     */
116 672
    private function createRequest(InternalRequestInterface $internalRequest, $success = null, $error = null)
117
    {
118 672
        $request = $this->client->createRequest(
119 672
            $internalRequest->getMethod(),
120 672
            (string) $internalRequest->getUri(),
121 672
            $this->prepareHeaders($internalRequest),
122 672
            $this->prepareContent($internalRequest),
123
            array(
124 672
                'exceptions'      => false,
125 504
                'allow_redirects' => false,
126 672
                'timeout'         => $this->getConfiguration()->getTimeout(),
127 672
                'connect_timeout' => $this->getConfiguration()->getTimeout(),
128
            )
129 504
        );
130
131 672
        $request->setProtocolVersion($internalRequest->getProtocolVersion());
132
133 672
        if (is_callable($success)) {
134 16
            $messageFactory = $this->getConfiguration()->getMessageFactory();
135
136 16
            $request->getEventDispatcher()->addListener(
137 16
                'request.success',
138
                function (Event $event) use ($messageFactory, $success, $internalRequest) {
139 16
                    $response = $messageFactory->createResponse(
140 16
                        $event['response']->getStatusCode(),
141 16
                        $event['response']->getProtocolVersion(),
142 16
                        $event['response']->getHeaders()->toArray(),
143 16
                        BodyNormalizer::normalize(
144
                            function () use ($event) {
145 16
                                $resource = $event['response']->getBody()->getStream();
146 16
                                $event['response']->getBody()->detachStream();
147
148 16
                                return $resource;
149 16
                            },
150 16
                            $internalRequest->getMethod()
151 12
                        )
152 12
                    );
153
154 16
                    $response = $response->withParameter('request', $internalRequest);
155 16
                    call_user_func($success, $response);
156 16
                }
157 12
            );
158 12
        }
159
160 672
        if (is_callable($error)) {
161 16
            $httpAdapterName = $this->getName();
162
163 16
            $request->getEventDispatcher()->addListener(
164 16
                'request.exception',
165 10
                function (Event $event) use ($error, $internalRequest, $httpAdapterName) {
166 8
                    $exception = HttpAdapterException::cannotFetchUri(
167 8
                        $event['exception']->getRequest()->getUrl(),
168 6
                        $httpAdapterName,
169 8
                        $event['exception']->getMessage()
170 6
                    );
171
172 8
                    $exception->setRequest($internalRequest);
173 8
                    call_user_func($error, $exception);
174 10
                }
175 12
            );
176 12
        }
177
178 672
        return $request;
179
    }
180
}
181