Code Duplication    Length = 62-65 lines in 2 locations

src/Zend1HttpAdapter.php 1 location

@@ 20-84 (lines=65) @@
17
/**
18
 * @author GeLo <[email protected]>
19
 */
20
class Zend1HttpAdapter extends AbstractHttpAdapter
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
    public function __construct(\Zend_Http_Client $client = null, ConfigurationInterface $configuration = null)
30
    {
31
        parent::__construct($configuration);
32
33
        $this->client = $client ?: new \Zend_Http_Client();
34
    }
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

src/Zend2HttpAdapter.php 1 location

@@ 22-83 (lines=62) @@
19
/**
20
 * @author GeLo <[email protected]>
21
 */
22
class Zend2HttpAdapter extends AbstractHttpAdapter
23
{
24
    /**
25
     * @var Client
26
     */
27
    private $client;
28
29
    /**
30
     * @param \Zend\Http\Client|null      $client
31
     * @param ConfigurationInterface|null $configuration
32
     */
33
    public function __construct(Client $client = null, ConfigurationInterface $configuration = null)
34
    {
35
        parent::__construct($configuration);
36
37
        $this->client = $client ?: new Client();
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getName()
44
    {
45
        return 'zend2';
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    protected function sendInternalRequest(InternalRequestInterface $internalRequest)
52
    {
53
        $this->client
54
            ->resetParameters(true)
55
            ->setOptions([
56
                'httpversion'  => $internalRequest->getProtocolVersion(),
57
                'timeout'      => $this->getConfiguration()->getTimeout(),
58
                'maxredirects' => 0,
59
            ])
60
            ->setUri($uri = (string) $internalRequest->getUri())
61
            ->setMethod($internalRequest->getMethod())
62
            ->setHeaders($this->prepareHeaders($internalRequest))
63
            ->setRawBody($this->prepareBody($internalRequest));
64
65
        try {
66
            $response = $this->client->send();
67
        } catch (\Exception $e) {
68
            throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage());
69
        }
70
71
        return $this->getConfiguration()->getMessageFactory()->createResponse(
72
            $response->getStatusCode(),
73
            $response->getVersion(),
74
            $response->getHeaders()->toArray(),
75
            BodyNormalizer::normalize(
76
                function () use ($response) {
77
                    return $response instanceof Stream ? $response->getStream() : $response->getBody();
78
                },
79
                $internalRequest->getMethod()
80
            )
81
        );
82
    }
83
}
84