Guzzle6HttpAdapter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 130
Duplicated Lines 28.46 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 84.88%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 14
dl 37
loc 130
ccs 73
cts 86
cp 0.8488
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() 27 27 2
B sendInternalRequests() 10 40 2
A createRequest() 0 10 1
A createOptions() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
Duplication introduced by GeLo
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) {
0 ignored issues
show
Duplication introduced by GeLo
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 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