Completed
Pull Request — master (#129)
by Eric
257:01 queued 192:06
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
 * Guzzle 6 http adapter.
24
 *
25
 * @author GeLo <[email protected]>
26
 */
27
class Guzzle6HttpAdapter extends AbstractHttpAdapter
28
{
29
    /** @var \GuzzleHttp\ClientInterface */
30
    private $client;
31
32
    /**
33
     * Creates a guzzle 6 http adapter.
34
     *
35
     * @param \GuzzleHttp\ClientInterface|null               $client        The guzzle 6 client.
36
     * @param \Ivory\HttpAdapter\ConfigurationInterface|null $configuration The configuration.
37
     */
38 3758
    public function __construct(ClientInterface $client = null, ConfigurationInterface $configuration = null)
39
    {
40 3758
        parent::__construct($configuration);
41
42 3758
        $this->client = $client ?: new Client();
43 3758
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 215
    public function getName()
49
    {
50 215
        return 'guzzle6';
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 3606
    protected function sendInternalRequest(InternalRequestInterface $internalRequest)
57
    {
58
        try {
59 3526
            $response = $this->client->send(
60 3526
                $this->createRequest($internalRequest),
61 3526
                $this->createOptions()
62 3280
            );
63 3289
        } catch (RequestException $e) {
64 129
            throw HttpAdapterException::cannotFetchUri(
65 143
                $e->getRequest()->getUri(),
66 129
                $this->getName(),
67 129
                $e->getMessage()
68 120
            );
69 14
        }
70
71 3397
        return $this->getConfiguration()->getMessageFactory()->createResponse(
72 3397
            (integer) $response->getStatusCode(),
73 3397
            $response->getProtocolVersion(),
74 3397
            $response->getHeaders(),
75 3397
            BodyNormalizer::normalize(
76
                function () use ($response) {
77 3139
                    return $response->getBody()->detach();
78 3397
                },
79 3597
                $internalRequest->getMethod()
80 3160
            )
81 3160
        );
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 3366
    protected function sendInternalRequests(array $internalRequests, $success, $error)
88
    {
89 86
        $requests = array();
90 86
        foreach ($internalRequests as $key => $internalRequest) {
91 86
            $requests[$key] = $this->createRequest($internalRequest);
92 80
        }
93
94 86
        $httpAdapter = $this;
95
96 86
        $pool = new Pool($this->client, $requests, array_merge($this->createOptions(), array(
97
            'fulfilled' => function ($response, $index) use ($success, $internalRequests, $httpAdapter) {
98 86
                $response = $httpAdapter->getConfiguration()->getMessageFactory()->createResponse(
99 86
                    (integer) $response->getStatusCode(),
100 86
                    $response->getProtocolVersion(),
101 86
                    $response->getHeaders(),
102 86
                    BodyNormalizer::normalize(
103
                        function () use ($response) {
104 86
                            return $response->getBody()->detach();
105 86
                        },
106 86
                        $internalRequests[$index]->getMethod()
107 80
                    )
108 80
                );
109
110 86
                $response = $response->withParameter('request', $internalRequests[$index]);
111 86
                call_user_func($success, $response);
112 86
            },
113 3366 View Code Duplication
            'rejected' => function ($exception, $index)  use ($error, $internalRequests, $httpAdapter) {
0 ignored issues
show
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...
114 43
                $exception = HttpAdapterException::cannotFetchUri(
115 43
                    $exception->getRequest()->getUri(),
116 43
                    $httpAdapter->getName(),
117 43
                    $exception->getMessage()
118 40
                );
119
120 43
                $exception->setRequest($internalRequests[$index]);
121 43
                call_user_func($error, $exception);
122 3366
            },
123 80
        )));
124
125 86
        $pool->promise()->wait();
126 86
    }
127
128
    /**
129
     * @param \Ivory\HttpAdapter\Message\InternalRequestInterface $internalRequest
130
     *
131
     * @return \GuzzleHttp\Psr7\Request
132
     */
133 3612
    private function createRequest(InternalRequestInterface $internalRequest)
134
    {
135 3612
        return new Request(
136 3612
            $internalRequest->getMethod(),
137 3612
            $internalRequest->getUri(),
138 3612
            $this->prepareHeaders($internalRequest),
139 3612
            $this->prepareBody($internalRequest),
140 3612
            $internalRequest->getProtocolVersion()
141 3360
        );
142
    }
143
144
    /**
145
     * @return array
146
     */
147 3612
    private function createOptions()
148
    {
149
        return array(
150 3612
            'http_errors'     => false,
151 3360
            'allow_redirects' => false,
152 3612
            'timeout'         => $this->getConfiguration()->getTimeout(),
153 3612
            'connect_timeout' => $this->getConfiguration()->getTimeout(),
154 3360
        );
155
    }
156
}
157