|
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; |
|
|
|
|
|
|
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
|
|
|
|
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
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. 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.