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

Zend1HttpAdapter::sendInternalRequest()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 35
Code Lines 26

Duplication

Lines 35
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 35
loc 35
ccs 0
cts 28
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 26
nc 2
nop 1
crap 12
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