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

RequestsHttpAdapter::sendInternalRequest()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 0
cts 24
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 23
nc 4
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
16
/**
17
 * @author GeLo <[email protected]>
18
 */
19
class RequestsHttpAdapter extends AbstractHttpAdapter
20
{
21
    /**
22
     * @var \Requests_Transport
23
     */
24
    private $transport;
25
26
    /**
27
     * @param \Requests_Transport|null    $transport
28
     * @param ConfigurationInterface|null $configuration
29
     */
30 18
    public function __construct(\Requests_Transport $transport = null, ConfigurationInterface $configuration = null)
31
    {
32 18
        parent::__construct($configuration);
33
34 18
        $this->transport = $transport;
35 18
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getName()
41
    {
42
        return 'requests';
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function sendInternalRequest(InternalRequestInterface $internalRequest)
49
    {
50
        $options = [
51
            'timeout'          => $this->getConfiguration()->getTimeout(),
52
            'connect_timeout'  => $this->getConfiguration()->getTimeout(),
53
            'protocol_version' => (float) $this->getConfiguration()->getProtocolVersion(),
54
            'follow_redirects' => 0,
55
            'data_format'      => 'body',
56
        ];
57
58
        if ($this->transport !== null) {
59
            $options['transport'] = $this->transport;
60
        }
61
62
        try {
63
            $response = \Requests::request(
64
                $uri = (string) $internalRequest->getUri(),
65
                $this->prepareHeaders($internalRequest),
66
                $this->prepareBody($internalRequest),
0 ignored issues
show
Documentation introduced by
$this->prepareBody($internalRequest) is of type string, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
                $internalRequest->getMethod(),
68
                $options
69
            );
70
        } catch (\Exception $e) {
71
            throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage());
72
        }
73
74
        return $this->getConfiguration()->getMessageFactory()->createResponse(
75
            $response->status_code,
76
            sprintf('%.1f', $response->protocol_version),
77
            $response->headers->getAll(),
78
            $response->body
79
        );
80
    }
81
}
82