HttpfulHttpAdapter::sendInternalRequest()   B
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 24
cts 24
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 24
nc 8
nop 1
crap 4
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 Httpful\Mime;
15
use Httpful\Request;
16
use Ivory\HttpAdapter\Extractor\ProtocolVersionExtractor;
17
use Ivory\HttpAdapter\Message\InternalRequestInterface;
18
use Ivory\HttpAdapter\Normalizer\BodyNormalizer;
19
20
/**
21
 * @author GeLo <[email protected]>
22
 */
23
class HttpfulHttpAdapter extends AbstractCurlHttpAdapter
24
{
25
    /**
26
     * @param ConfigurationInterface|null $configuration
27
     */
28 705
    public function __construct(ConfigurationInterface $configuration = null)
29
    {
30 705
        parent::__construct($configuration);
31 704
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 41
    public function getName()
37
    {
38 41
        return 'httpful';
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 672
    protected function sendInternalRequest(InternalRequestInterface $internalRequest)
45
    {
46 672
        $request = Request::init($internalRequest->getMethod())
47 672
            ->whenError(function () {
48 672
            })
49 672
            ->addOnCurlOption(CURLOPT_HTTP_VERSION, $this->prepareProtocolVersion($internalRequest))
50 672
            ->timeout($this->getConfiguration()->getTimeout())
51 672
            ->uri($uri = (string) $internalRequest->getUri())
52 672
            ->addHeaders($this->prepareHeaders($internalRequest))
53 672
            ->body($this->prepareContent($internalRequest));
54
55 672
        if (defined('CURLOPT_CONNECTTIMEOUT_MS')) {
56 672
            $request->addOnCurlOption(CURLOPT_CONNECTTIMEOUT_MS, $this->getConfiguration()->getTimeout() * 1000);
57
        } else { // @codeCoverageIgnoreStart
58
            $request->addOnCurlOption(CURLOPT_CONNECTTIMEOUT, $this->getConfiguration()->getTimeout());
59
        } // @codeCoverageIgnoreEnd
60
61 672
        $files = $internalRequest->getFiles();
62
63 672
        if (!empty($files)) {
64 80
            $request->mime(Mime::UPLOAD);
65 60
        }
66
67
        try {
68 672
            $response = $request->send();
69 512
        } catch (\Exception $e) {
70 32
            throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage());
71
        }
72
73 648
        return $this->getConfiguration()->getMessageFactory()->createResponse(
74 648
            $response->code,
75 666
            ProtocolVersionExtractor::extract($response->raw_headers),
76 648
            $response->headers->toArray(),
77 648
            BodyNormalizer::normalize($response->body, $internalRequest->getMethod())
78 486
        );
79
    }
80
}
81