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

Guzzle6HttpAdapter::getName()   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 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 GuzzleHttp\Client;
15
use GuzzleHttp\ClientInterface;
16
use GuzzleHttp\Exception\RequestException;
17
use GuzzleHttp\Pool;
18
use GuzzleHttp\Psr7\Request;
19
use Ivory\HttpAdapter\Message\InternalRequestInterface;
20
use Ivory\HttpAdapter\Normalizer\BodyNormalizer;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25
class Guzzle6HttpAdapter extends AbstractHttpAdapter
26
{
27
    /**
28
     * @var ClientInterface
29
     */
30
    private $client;
31
32
    /**
33
     * @param ClientInterface|null        $client
34
     * @param ConfigurationInterface|null $configuration
35
     */
36 20
    public function __construct(ClientInterface $client = null, ConfigurationInterface $configuration = null)
37
    {
38 20
        parent::__construct($configuration);
39
40 20
        $this->client = $client ?: new Client();
41 20
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getName()
47
    {
48
        return 'guzzle6';
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 3 View Code Duplication
    protected function sendInternalRequest(InternalRequestInterface $internalRequest)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        try {
57
            $response = $this->client->send(
58
                $this->createRequest($internalRequest),
59
                $this->createOptions()
60
            );
61
        } catch (RequestException $e) {
62
            throw HttpAdapterException::cannotFetchUri(
63
                $e->getRequest()->getUri(),
64
                $this->getName(),
65 3
                $e->getMessage()
66
            );
67
        }
68
69
        return $this->getConfiguration()->getMessageFactory()->createResponse(
70
            (int) $response->getStatusCode(),
71
            $response->getProtocolVersion(),
72
            $response->getHeaders(),
73
            BodyNormalizer::normalize(
74
                function () use ($response) {
75
                    return $response->getBody()->detach();
76
                },
77
                $internalRequest->getMethod()
78
            )
79
        );
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    protected function sendInternalRequests(array $internalRequests, $success, $error)
86
    {
87
        $requests = [];
88
        foreach ($internalRequests as $key => $internalRequest) {
89
            $requests[$key] = $this->createRequest($internalRequest);
90
        }
91
92
        $httpAdapter = $this;
93
94
        $pool = new Pool($this->client, $requests, array_merge($this->createOptions(), [
95
            'fulfilled' => function ($response, $index) use ($success, $internalRequests, $httpAdapter) {
96
                $response = $httpAdapter->getConfiguration()->getMessageFactory()->createResponse(
97
                    (int) $response->getStatusCode(),
98
                    $response->getProtocolVersion(),
99
                    $response->getHeaders(),
100
                    BodyNormalizer::normalize(
101
                        function () use ($response) {
102
                            return $response->getBody()->detach();
103
                        },
104
                        $internalRequests[$index]->getMethod()
105
                    )
106
                );
107
108
                $response = $response->withParameter('request', $internalRequests[$index]);
109
                call_user_func($success, $response);
110
            },
111 View Code Duplication
            'rejected' => function ($exception, $index) use ($error, $internalRequests, $httpAdapter) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
                $exception = HttpAdapterException::cannotFetchUri(
113
                    $exception->getRequest()->getUri(),
114
                    $httpAdapter->getName(),
115
                    $exception->getMessage()
116
                );
117
118
                $exception->setRequest($internalRequests[$index]);
119
                call_user_func($error, $exception);
120
            },
121
        ]));
122
123
        $pool->promise()->wait();
124
    }
125
126
    /**
127
     * @param InternalRequestInterface $internalRequest
128
     *
129
     * @return Request
130
     */
131
    private function createRequest(InternalRequestInterface $internalRequest)
132
    {
133
        return new Request(
134
            $internalRequest->getMethod(),
135
            $internalRequest->getUri(),
136
            $this->prepareHeaders($internalRequest),
137
            $this->prepareBody($internalRequest),
138
            $internalRequest->getProtocolVersion()
139
        );
140
    }
141
142
    /**
143
     * @return array
144
     */
145
    private function createOptions()
146
    {
147
        return [
148
            'http_errors'     => false,
149
            'allow_redirects' => false,
150
            'timeout'         => $this->getConfiguration()->getTimeout(),
151
            'connect_timeout' => $this->getConfiguration()->getTimeout(),
152
        ];
153
    }
154
}
155