Completed
Push — master ( c8f54d...0a9660 )
by Eric
21:26 queued 01:31
created

src/Guzzle6HttpAdapter.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 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 1138
    public function __construct(ClientInterface $client = null, ConfigurationInterface $configuration = null)
37
    {
38 1138
        parent::__construct($configuration);
39
40 1138
        $this->client = $client ?: new Client();
41 1138
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 65
    public function getName()
47
    {
48 65
        return 'guzzle6';
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1080 View Code Duplication
    protected function sendInternalRequest(InternalRequestInterface $internalRequest)
0 ignored issues
show
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 1066
            $response = $this->client->send(
58 1066
                $this->createRequest($internalRequest),
59 1066
                $this->createOptions()
60 574
            );
61 592
        } catch (RequestException $e) {
62 39
            throw HttpAdapterException::cannotFetchUri(
63 39
                $e->getRequest()->getUri(),
64 39
                $this->getName(),
65 42
                $e->getMessage()
66 21
            );
67
        }
68
69 1030
        return $this->getConfiguration()->getMessageFactory()->createResponse(
70 1027
            (int) $response->getStatusCode(),
71 1027
            $response->getProtocolVersion(),
72 1027
            $response->getHeaders(),
73 1027
            BodyNormalizer::normalize(
74
                function () use ($response) {
75 1026
                    return $response->getBody()->detach();
76 1027
                },
77 1027
                $internalRequest->getMethod()
78 553
            )
79 553
        );
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 600
    protected function sendInternalRequests(array $internalRequests, $success, $error)
86
    {
87 26
        $requests = [];
88 26
        foreach ($internalRequests as $key => $internalRequest) {
89 26
            $requests[$key] = $this->createRequest($internalRequest);
90 14
        }
91
92 26
        $httpAdapter = $this;
93
94 26
        $pool = new Pool($this->client, $requests, array_merge($this->createOptions(), [
95
            'fulfilled' => function ($response, $index) use ($success, $internalRequests, $httpAdapter) {
96 26
                $response = $httpAdapter->getConfiguration()->getMessageFactory()->createResponse(
97 26
                    (int) $response->getStatusCode(),
98 26
                    $response->getProtocolVersion(),
99 26
                    $response->getHeaders(),
100 26
                    BodyNormalizer::normalize(
101
                        function () use ($response) {
102 26
                            return $response->getBody()->detach();
103 29
                        },
104 26
                        $internalRequests[$index]->getMethod()
105 14
                    )
106 14
                );
107
108 26
                $response = $response->withParameter('request', $internalRequests[$index]);
109 26
                call_user_func($success, $response);
110 26
            },
111 600 View Code Duplication
            'rejected' => function ($exception, $index) use ($error, $internalRequests, $httpAdapter) {
112 13
                $exception = HttpAdapterException::cannotFetchUri(
113 13
                    $exception->getRequest()->getUri(),
114 13
                    $httpAdapter->getName(),
115 13
                    $exception->getMessage()
116 588
                );
117
118 13
                $exception->setRequest($internalRequests[$index]);
119 13
                call_user_func($error, $exception);
120 26
            },
121 14
        ]));
122
123 26
        $pool->promise()->wait();
124 26
    }
125
126
    /**
127
     * @param InternalRequestInterface $internalRequest
128
     *
129
     * @return Request
130
     */
131 1092
    private function createRequest(InternalRequestInterface $internalRequest)
132
    {
133 1092
        return new Request(
134 1092
            $internalRequest->getMethod(),
135 1092
            $internalRequest->getUri(),
136 1092
            $this->prepareHeaders($internalRequest),
137 1092
            $this->prepareBody($internalRequest),
138 1092
            $internalRequest->getProtocolVersion()
139 588
        );
140
    }
141
142
    /**
143
     * @return array
144
     */
145 1092
    private function createOptions()
146
    {
147
        return [
148 1092
            'http_errors'     => false,
149 588
            'allow_redirects' => false,
150 1092
            'timeout'         => $this->getConfiguration()->getTimeout(),
151 1092
            'connect_timeout' => $this->getConfiguration()->getTimeout(),
152 588
        ];
153
    }
154
}
155