Completed
Push — master ( 69d505...6c8f28 )
by Markus
04:01 queued 01:33
created

Curl::send()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 0
cts 15
cp 0
rs 8.7972
cc 4
eloc 14
nc 3
nop 1
crap 20
1
<?php
2
3
namespace Fhp\Adapter;
4
5
use Fhp\Adapter\Exception\AdapterException;
6
use Fhp\Adapter\Exception\CurlException;
7
use Fhp\Message\AbstractMessage;
8
9
/**
10
 * Class Curl
11
 * @package Fhp\Adapter
12
 */
13
class Curl implements AdapterInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $host;
19
20
    /**
21
     * @var int
22
     */
23
    protected $port;
24
25
    /**
26
     * @var resource
27
     */
28
    protected $curlHandle;
29
30
    /**
31
     * @var mixed
32
     */
33
    protected $lastResponseInfo;
34
35
    /**
36
     * Curl constructor.
37
     *
38
     * @param string $host
39
     * @param int $port
40
     * @throws AdapterException
41
     * @throws CurlException
42
     */
43
    public function __construct($host, $port)
44
    {
45
        if (!is_numeric($port) || (int) $port <= 0) {
46
            throw new AdapterException('Invalid port number');
47
        }
48
49
        $this->host = $host;
50
        $this->port = $port;
0 ignored issues
show
Documentation Bug introduced by
It seems like $port can also be of type double or string. However, the property $port is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
51
        $this->curlHandle = curl_init();
52
        
53
        curl_setopt($this->curlHandle, CURLOPT_SSLVERSION, 1);
54
        curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, true);
55
        curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYHOST, 2);
56
        curl_setopt($this->curlHandle, CURLOPT_USERAGENT, "FHP-lib");
57
        curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true);
58
        curl_setopt($this->curlHandle, CURLOPT_URL, $this->host);
59
        curl_setopt($this->curlHandle, CURLOPT_CONNECTTIMEOUT, 15);
60
        curl_setopt($this->curlHandle, CURLOPT_CUSTOMREQUEST, 'POST');
61
        curl_setopt($this->curlHandle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
62
        curl_setopt($this->curlHandle, CURLOPT_ENCODING, '');
63
        curl_setopt($this->curlHandle, CURLOPT_MAXREDIRS, 0);
64
        curl_setopt($this->curlHandle, CURLOPT_TIMEOUT, 30);
65
        curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, array("cache-control: no-cache", 'Content-Type: text/plain'));
66
    }
67
68
    /**
69
     * @param AbstractMessage $message
70
     * @return string
71
     * @throws CurlException
72
     */
73
    public function send(AbstractMessage $message)
74
    {
75
        curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, base64_encode($message->toString()));
76
        $response = curl_exec($this->curlHandle);
77
        $this->lastResponseInfo = curl_getinfo($this->curlHandle);
78
79
        if (false === $response) {
80
            throw new CurlException(
81
                'Failed connection to ' . $this->host . ': ' . curl_error($this->curlHandle),
82
                curl_errno($this->curlHandle),
83
                null,
84
                $this->lastResponseInfo
85
            );
86
        }
87
88
        $statusCode = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
89
90
        if ($statusCode < 200 || $statusCode > 299) {
91
            throw new CurlException('Bad response with status code ' . $statusCode, 0, null, $this->lastResponseInfo);
92
        }
93
94
        return base64_decode($response);
95
    }
96
97
    /**
98
     * Gets curl info for last request / response.
99
     *
100
     * @return mixed
101
     */
102
    public function getLastResponseInfo()
103
    {
104
        return $this->lastResponseInfo;
105
    }
106
}
107