Guzzle3HttpAdapter::getName()   A
last analyzed

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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 705
    public function __construct(ClientInterface $client = null, ConfigurationInterface $configuration = null)
36
    {
37 705
        parent::__construct($configuration);
38
39 704
        $this->client = $client ?: new Client();
40 704
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 49
    public function getName()
46
    {
47 49
        return 'guzzle3';
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 674
    protected function sendInternalRequest(InternalRequestInterface $internalRequest)
54
    {
55
        try {
56 656
            $response = $this->createRequest($internalRequest)->send();
57 498
        } catch (RequestException $e) {
58 24
            throw HttpAdapterException::cannotFetchUri(
59 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...
60 24
                $this->getName(),
61 24
                $e->getMessage()
62 18
            );
63
        }
64
65 632
        return $this->getConfiguration()->getMessageFactory()->createResponse(
66 632
            $response->getStatusCode(),
67 632
            $response->getProtocolVersion(),
68 632
            $response->getHeaders()->toArray(),
69 632
            BodyNormalizer::normalize(
70
                function () use ($response) {
71 584
                    $resource = $response->getBody()->getStream();
72 584
                    $response->getBody()->detachStream();
73
74 584
                    return $resource;
75 668
                },
76 632
                $internalRequest->getMethod()
77 474
            )
78 474
        );
79 6
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 16
    protected function sendInternalRequests(array $internalRequests, $success, $error)
85
    {
86 16
        $requests = [];
87 16
        foreach ($internalRequests as $internalRequest) {
88 16
            $requests[] = $this->createRequest($internalRequest, $success, $error);
89 12
        }
90
91
        try {
92 16
            $this->client->send($requests);
93 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...
94
        }
95 16
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 80
    protected function createFile($file)
101
    {
102 80
        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 672
    private function createRequest(InternalRequestInterface $internalRequest, $success = null, $error = null)
113
    {
114 672
        $request = $this->client->createRequest(
115 672
            $internalRequest->getMethod(),
116 672
            (string) $internalRequest->getUri(),
117 672
            $this->prepareHeaders($internalRequest),
118 672
            $this->prepareContent($internalRequest),
119
            [
120 672
                'exceptions'      => false,
121 504
                'allow_redirects' => false,
122 672
                'timeout'         => $this->getConfiguration()->getTimeout(),
123 672
                'connect_timeout' => $this->getConfiguration()->getTimeout(),
124
            ]
125 504
        );
126
127 672
        $request->setProtocolVersion($internalRequest->getProtocolVersion());
128
129 672
        if (is_callable($success)) {
130 16
            $messageFactory = $this->getConfiguration()->getMessageFactory();
131
132 16
            $request->getEventDispatcher()->addListener(
133 16
                'request.success',
134
                function (Event $event) use ($messageFactory, $success, $internalRequest) {
135 16
                    $response = $messageFactory->createResponse(
136 16
                        $event['response']->getStatusCode(),
137 16
                        $event['response']->getProtocolVersion(),
138 16
                        $event['response']->getHeaders()->toArray(),
139 16
                        BodyNormalizer::normalize(
140
                            function () use ($event) {
141 16
                                $resource = $event['response']->getBody()->getStream();
142 16
                                $event['response']->getBody()->detachStream();
143
144 16
                                return $resource;
145 16
                            },
146 16
                            $internalRequest->getMethod()
147 12
                        )
148 12
                    );
149
150 16
                    $response = $response->withParameter('request', $internalRequest);
151 16
                    call_user_func($success, $response);
152 16
                }
153 12
            );
154 12
        }
155
156 672
        if (is_callable($error)) {
157 16
            $httpAdapterName = $this->getName();
158
159 16
            $request->getEventDispatcher()->addListener(
160 16
                'request.exception',
161 10
                function (Event $event) use ($error, $internalRequest, $httpAdapterName) {
162 8
                    $exception = HttpAdapterException::cannotFetchUri(
163 8
                        $event['exception']->getRequest()->getUrl(),
164 6
                        $httpAdapterName,
165 8
                        $event['exception']->getMessage()
166 6
                    );
167
168 8
                    $exception->setRequest($internalRequest);
169 8
                    call_user_func($error, $exception);
170 10
                }
171 12
            );
172 12
        }
173
174 672
        return $request;
175
    }
176
}
177