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

Zend1HttpAdapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 11.76%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 9
dl 63
loc 63
ccs 4
cts 34
cp 0.1176
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 2
A getName() 4 4 1
B sendInternalRequest() 35 35 3

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 Ivory\HttpAdapter\Message\InternalRequestInterface;
15
use Ivory\HttpAdapter\Normalizer\BodyNormalizer;
16
17
/**
18
 * @author GeLo <[email protected]>
19
 */
20 View Code Duplication
class Zend1HttpAdapter extends AbstractHttpAdapter
0 ignored issues
show
Duplication introduced by
This class 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...
21
{
22
    /** @var \Zend_Http_Client */
23
    private $client;
24
25
    /**
26
     * @param \Zend_Http_Client|null      $client
27
     * @param ConfigurationInterface|null $configuration
28
     */
29 18
    public function __construct(\Zend_Http_Client $client = null, ConfigurationInterface $configuration = null)
30
    {
31 18
        parent::__construct($configuration);
32
33 18
        $this->client = $client ?: new \Zend_Http_Client();
34 18
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getName()
40
    {
41
        return 'zend1';
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function sendInternalRequest(InternalRequestInterface $internalRequest)
48
    {
49
        $this->client
50
            ->resetParameters(true)
51
            ->setConfig([
52
                'httpversion'     => $internalRequest->getProtocolVersion(),
53
                'timeout'         => $this->getConfiguration()->getTimeout(),
54
                'request_timeout' => $this->getConfiguration()->getTimeout(),
55
                'maxredirects'    => 0,
56
            ])
57
            ->setUri($uri = (string) $internalRequest->getUri())
58
            ->setMethod($internalRequest->getMethod())
59
            ->setHeaders($this->prepareHeaders($internalRequest))
60
            ->setRawData($this->prepareBody($internalRequest));
61
62
        try {
63
            $response = $this->client->request();
64
        } catch (\Exception $e) {
65
            throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage());
66
        }
67
68
        return $this->getConfiguration()->getMessageFactory()->createResponse(
69
            $response->getStatus(),
70
            $response->getVersion(),
71
            $response->getHeaders(),
72
            BodyNormalizer::normalize(
73
                function () use ($response) {
74
                    return $response instanceof \Zend_Http_Response_Stream
75
                        ? $response->getStream()
76
                        : $response->getBody();
77
                },
78
                $internalRequest->getMethod()
79
            )
80
        );
81
    }
82
}
83