Completed
Pull Request — master (#129)
by Eric
257:01 queued 192:06
created

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