1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bpost\BpostApiClient\ApiCaller; |
4
|
|
|
|
5
|
|
|
use Bpost\BpostApiClient\Exception\BpostApiResponseException; |
6
|
|
|
use Bpost\BpostApiClient\Exception\BpostApiResponseException\BpostApiBusinessException; |
7
|
|
|
use Bpost\BpostApiClient\Exception\BpostApiResponseException\BpostApiSystemException; |
8
|
|
|
use Bpost\BpostApiClient\Exception\BpostApiResponseException\BpostCurlException; |
9
|
|
|
use Bpost\BpostApiClient\Exception\BpostApiResponseException\BpostInvalidResponseException; |
10
|
|
|
use Bpost\BpostApiClient\Exception\BpostApiResponseException\BpostInvalidXmlResponseException; |
11
|
|
|
use Bpost\BpostApiClient\Logger; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ApiCaller |
15
|
|
|
* @package Bpost\BpostApiClient\ApiCaller |
16
|
|
|
* @codeCoverageIgnore That makes a HTTP request with the bpost API |
17
|
|
|
*/ |
18
|
|
|
class ApiCaller |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** @var Logger */ |
22
|
|
|
private $logger; |
23
|
|
|
|
24
|
|
|
/** @var int */ |
25
|
|
|
private $responseHttpCode; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
private $responseBody; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
private $responseContentType; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* ApiCaller constructor. |
35
|
|
|
* @param Logger $logger |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
public function __construct(Logger $logger = null) |
38
|
|
|
{ |
39
|
|
|
$this->logger = $logger; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return int |
44
|
|
|
*/ |
45
|
|
|
public function getResponseHttpCode() |
46
|
|
|
{ |
47
|
|
|
return $this->responseHttpCode; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function getResponseBody() |
54
|
|
|
{ |
55
|
|
|
return $this->responseBody; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getResponseContentType() |
62
|
|
|
{ |
63
|
|
|
return $this->responseContentType; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array $options |
69
|
|
|
* @return bool |
70
|
|
|
* @throws BpostApiBusinessException |
71
|
|
|
* @throws BpostApiResponseException |
72
|
|
|
* @throws BpostApiSystemException |
73
|
|
|
* @throws BpostCurlException |
74
|
|
|
* @throws BpostInvalidResponseException |
75
|
|
|
* @throws BpostInvalidXmlResponseException |
76
|
|
|
*/ |
77
|
|
|
public function doCall(array $options) |
78
|
|
|
{ |
79
|
|
|
$curl = curl_init(); |
80
|
|
|
|
81
|
|
|
// set options |
82
|
|
|
curl_setopt_array($curl, $options); |
83
|
|
|
|
84
|
|
|
$this->logger->debug('curl request', $options); |
85
|
|
|
|
86
|
|
|
// execute |
87
|
|
|
$this->responseBody = curl_exec($curl); |
88
|
|
|
$errorNumber = curl_errno($curl); |
89
|
|
|
$errorMessage = curl_error($curl); |
90
|
|
|
|
91
|
|
|
$headers = curl_getinfo($curl); |
92
|
|
|
|
93
|
|
|
$this->logger->debug('curl response', array( |
94
|
|
|
'status' => $errorNumber . ' (' . $errorMessage . ')', |
95
|
|
|
'headers' => $headers, |
96
|
|
|
'response' => $this->responseBody |
97
|
|
|
)); |
98
|
|
|
|
99
|
|
|
// error? |
100
|
|
|
if ($errorNumber != '') { |
101
|
|
|
throw new BpostCurlException($errorMessage, $errorNumber); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (isset($headers['http_code'])) { |
105
|
|
|
$this->responseHttpCode = $headers['http_code']; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (isset($headers['Content-Type'])) { |
109
|
|
|
$this->responseContentType = $headers['Content-Type']; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* If the httpCode is 200, return 200 |
117
|
|
|
* If the httpCode is 203, return 200 |
118
|
|
|
* If the httpCode is 404 ,return 404 |
119
|
|
|
* ... |
120
|
|
|
* |
121
|
|
|
* @return int |
122
|
|
|
*/ |
123
|
|
|
public function getHttpCodeType() |
124
|
|
|
{ |
125
|
|
|
return 100 * (int)($this->responseHttpCode / 100); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.