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

Guzzle3HttpAdapter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 10.47%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 11
dl 0
loc 153
ccs 9
cts 86
cp 0.1047
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getName() 0 4 1
B sendInternalRequest() 0 27 2
A sendInternalRequests() 0 12 3
A createFile() 0 4 1
A createRequest() 0 64 3
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
 * @author GeLo <[email protected]>
23
 */
24
class Guzzle3HttpAdapter extends AbstractCurlHttpAdapter
25
{
26
    /**
27
     * @var ClientInterface
28
     */
29
    private $client;
30
31
    /**
32
     * @param ClientInterface|null        $client
33
     * @param ConfigurationInterface|null $configuration
34
     */
35 17
    public function __construct(ClientInterface $client = null, ConfigurationInterface $configuration = null)
36
    {
37 17
        parent::__construct($configuration);
38
39 16
        $this->client = $client ?: new Client();
40 16
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    public function getName()
46
    {
47 1
        return 'guzzle3';
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 6
    protected function sendInternalRequest(InternalRequestInterface $internalRequest)
54
    {
55
        try {
56
            $response = $this->createRequest($internalRequest)->send();
57
        } catch (RequestException $e) {
58
            throw HttpAdapterException::cannotFetchUri(
59
                $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...
60
                $this->getName(),
61
                $e->getMessage()
62
            );
63
        }
64
65
        return $this->getConfiguration()->getMessageFactory()->createResponse(
66
            $response->getStatusCode(),
67
            $response->getProtocolVersion(),
68
            $response->getHeaders()->toArray(),
69
            BodyNormalizer::normalize(
70
                function () use ($response) {
71
                    $resource = $response->getBody()->getStream();
72
                    $response->getBody()->detachStream();
73
74
                    return $resource;
75 6
                },
76
                $internalRequest->getMethod()
77
            )
78
        );
79 6
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    protected function sendInternalRequests(array $internalRequests, $success, $error)
85
    {
86
        $requests = [];
87
        foreach ($internalRequests as $internalRequest) {
88
            $requests[] = $this->createRequest($internalRequest, $success, $error);
89
        }
90
91
        try {
92
            $this->client->send($requests);
93
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
94
        }
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    protected function createFile($file)
101
    {
102
        return '@'.$file;
103
    }
104
105
    /**
106
     * @param InternalRequestInterface $internalRequest
107
     * @param callable|null            $success
108
     * @param callable|null            $error
109
     *
110
     * @return RequestInterface
111
     */
112
    private function createRequest(InternalRequestInterface $internalRequest, $success = null, $error = null)
113
    {
114
        $request = $this->client->createRequest(
115
            $internalRequest->getMethod(),
116
            (string) $internalRequest->getUri(),
117
            $this->prepareHeaders($internalRequest),
118
            $this->prepareContent($internalRequest),
119
            [
120
                'exceptions'      => false,
121
                'allow_redirects' => false,
122
                'timeout'         => $this->getConfiguration()->getTimeout(),
123
                'connect_timeout' => $this->getConfiguration()->getTimeout(),
124
            ]
125
        );
126
127
        $request->setProtocolVersion($internalRequest->getProtocolVersion());
128
129
        if (is_callable($success)) {
130
            $messageFactory = $this->getConfiguration()->getMessageFactory();
131
132
            $request->getEventDispatcher()->addListener(
133
                'request.success',
134
                function (Event $event) use ($messageFactory, $success, $internalRequest) {
135
                    $response = $messageFactory->createResponse(
136
                        $event['response']->getStatusCode(),
137
                        $event['response']->getProtocolVersion(),
138
                        $event['response']->getHeaders()->toArray(),
139
                        BodyNormalizer::normalize(
140
                            function () use ($event) {
141
                                $resource = $event['response']->getBody()->getStream();
142
                                $event['response']->getBody()->detachStream();
143
144
                                return $resource;
145
                            },
146
                            $internalRequest->getMethod()
147
                        )
148
                    );
149
150
                    $response = $response->withParameter('request', $internalRequest);
151
                    call_user_func($success, $response);
152
                }
153
            );
154
        }
155
156
        if (is_callable($error)) {
157
            $httpAdapterName = $this->getName();
158
159
            $request->getEventDispatcher()->addListener(
160
                'request.exception',
161
                function (Event $event) use ($error, $internalRequest, $httpAdapterName) {
162
                    $exception = HttpAdapterException::cannotFetchUri(
163
                        $event['exception']->getRequest()->getUrl(),
164
                        $httpAdapterName,
165
                        $event['exception']->getMessage()
166
                    );
167
168
                    $exception->setRequest($internalRequest);
169
                    call_user_func($error, $exception);
170
                }
171
            );
172
        }
173
174
        return $request;
175
    }
176
}
177