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 | * |
||
16 | * @codeCoverageIgnore That makes a HTTP request with the bpost API |
||
17 | */ |
||
18 | class ApiCaller |
||
19 | { |
||
20 | /** @var Logger */ |
||
21 | private $logger; |
||
22 | |||
23 | /** @var int */ |
||
24 | private $responseHttpCode; |
||
25 | |||
26 | /** @var string */ |
||
27 | private $responseBody; |
||
28 | |||
29 | /** @var string */ |
||
30 | private $responseContentType; |
||
31 | |||
32 | /** |
||
33 | * ApiCaller constructor. |
||
34 | * |
||
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 | * @param array $options |
||
68 | * |
||
69 | * @return bool |
||
70 | * |
||
71 | * @throws BpostApiBusinessException |
||
72 | * @throws BpostApiResponseException |
||
73 | * @throws BpostApiSystemException |
||
74 | * @throws BpostCurlException |
||
75 | * @throws BpostInvalidResponseException |
||
76 | * @throws BpostInvalidXmlResponseException |
||
77 | */ |
||
78 | public function doCall(array $options) |
||
79 | { |
||
80 | $curl = curl_init(); |
||
81 | |||
82 | // set options |
||
83 | curl_setopt_array($curl, $options); |
||
84 | |||
85 | $this->logger->debug('curl request', $options); |
||
86 | |||
87 | // execute |
||
88 | $this->responseBody = curl_exec($curl); |
||
0 ignored issues
–
show
|
|||
89 | $errorNumber = curl_errno($curl); |
||
90 | $errorMessage = curl_error($curl); |
||
91 | |||
92 | $headers = curl_getinfo($curl); |
||
93 | |||
94 | $this->logger->debug('curl response', array( |
||
95 | 'status' => $errorNumber . ' (' . $errorMessage . ')', |
||
96 | 'headers' => $headers, |
||
97 | 'response' => $this->responseBody, |
||
98 | )); |
||
99 | |||
100 | // error? |
||
101 | if ($errorNumber !== 0) { |
||
102 | throw new BpostCurlException($errorMessage, $errorNumber); |
||
103 | } |
||
104 | |||
105 | if (isset($headers['http_code'])) { |
||
106 | $this->responseHttpCode = $headers['http_code']; |
||
107 | } |
||
108 | |||
109 | if (isset($headers['Content-Type'])) { |
||
110 | $this->responseContentType = $headers['Content-Type']; |
||
111 | } |
||
112 | |||
113 | return true; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * If the httpCode is 200, return 200 |
||
118 | * If the httpCode is 203, return 200 |
||
119 | * If the httpCode is 404 ,return 404 |
||
120 | * ... |
||
121 | * |
||
122 | * @return int |
||
123 | */ |
||
124 | public function getHttpCodeType() |
||
125 | { |
||
126 | return 100 * (int) ($this->responseHttpCode / 100); |
||
127 | } |
||
128 | } |
||
129 |
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 theid
property of an instance of theAccount
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.