PeclHttpAdapter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 79.49%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 66
ccs 31
cts 39
cp 0.7949
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
B sendInternalRequest() 0 36 3
A getName() 0 4 1
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 http\Client;
15
use http\Client\Request;
16
use http\Message\Body;
17
use Ivory\HttpAdapter\Message\InternalRequestInterface;
18
19
/**
20
 * @author GeLo <[email protected]>
21
 */
22
class PeclHttpAdapter extends AbstractHttpAdapter
23
{
24
    /**
25
     * @var Client
26
     */
27
    private $client;
28
29
    /**
30
     * @param Client                 $client
31
     * @param ConfigurationInterface $configuration
32
     */
33 616
    public function __construct(Client $client = null, ConfigurationInterface $configuration = null)
34
    {
35 616
        parent::__construct($configuration);
36
37 616
        $this->client = $client ?: new Client();
38 616
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 588
    protected function sendInternalRequest(InternalRequestInterface $internalRequest)
44
    {
45 588
        $body = new Body();
46 588
        $body->append($this->prepareBody($internalRequest));
47
48 588
        $request = new Request(
49 588
            $internalRequest->getMethod(),
50 588
            $uri = (string) $internalRequest->getUri(),
51 588
            $this->prepareHeaders($internalRequest),
52
            $body
53 588
        );
54
55 588
        $httpVersion = $internalRequest->getProtocolVersion() === InternalRequestInterface::PROTOCOL_VERSION_1_0
56 588
            ? \http\Client\Curl\HTTP_VERSION_1_0
57 588
            : \http\Client\Curl\HTTP_VERSION_1_1;
58
59 588
        $request->setOptions([
60 588
            'protocol' => $httpVersion,
61 588
            'timeout'  => $this->getConfiguration()->getTimeout(),
62 588
        ]);
63
64
        try {
65 588
            $this->client->reset()->enqueue($request)->send();
66 588
        } catch (\Exception $e) {
67 28
            throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage());
68
        }
69
70 567
        $response = $this->client->getResponse();
71
72 567
        return $this->getConfiguration()->getMessageFactory()->createResponse(
73 567
            $response->getResponseCode(),
74 567
            $response->getHttpVersion(),
75 588
            $response->getHeaders(),
76 567
            $response->getBody()->getResource()
77 567
        );
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 35
    public function getName()
84
    {
85 35
        return 'pecl_http';
86
    }
87
}
88