1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Bpost\BpostApiClient\ApiCaller; |
5
|
|
|
|
6
|
|
|
use Bpost\BpostApiClient\Exception\BpostApiResponseException\BpostCurlException; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Psr\Log\NullLogger; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ApiCaller |
12
|
|
|
* |
13
|
|
|
* @codeCoverageIgnore That makes a HTTP request with the bpost API |
14
|
|
|
*/ |
15
|
|
|
class ApiCaller |
16
|
|
|
{ |
17
|
|
|
private ?int $responseHttpCode = null; |
18
|
|
|
private string $responseBody = ''; |
19
|
|
|
private ?string $responseContentType = null; |
20
|
|
|
|
21
|
|
|
public function __construct( |
22
|
|
|
private readonly LoggerInterface $logger = new NullLogger() |
23
|
|
|
) {} |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
public function getResponseHttpCode(): ?int |
27
|
|
|
{ |
28
|
|
|
return $this->responseHttpCode; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getResponseBody(): string |
32
|
|
|
{ |
33
|
|
|
return $this->responseBody; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getResponseContentType(): ?string |
37
|
|
|
{ |
38
|
|
|
return $this->responseContentType; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @throws BpostCurlException |
43
|
|
|
*/ |
44
|
|
|
public function doCall(array $options): bool |
45
|
|
|
{ |
46
|
|
|
$curl = curl_init(); |
47
|
|
|
if (!$curl instanceof \CurlHandle) { |
48
|
|
|
throw new BpostCurlException('Unable to initialize cURL'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
curl_setopt_array($curl, $options); |
52
|
|
|
|
53
|
|
|
$this->logger->debug('curl request', $options); |
54
|
|
|
|
55
|
|
|
try { |
56
|
|
|
$result = curl_exec($curl); |
57
|
|
|
$errno = curl_errno($curl); |
58
|
|
|
$error = curl_error($curl); |
59
|
|
|
|
60
|
|
|
$info = curl_getinfo($curl); // array<string,mixed> |
61
|
|
|
|
62
|
|
|
$this->logger->debug('curl response', [ |
63
|
|
|
'status' => $errno . ' (' . $error . ')', |
64
|
|
|
'headers' => $info, |
65
|
|
|
'response' => $result, |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
if ($errno !== 0) { |
69
|
|
|
throw new BpostCurlException($error !== '' ? $error : 'cURL error', $errno); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// seulement maintenant qu'on sait que ce n’est pas une erreur |
73
|
|
|
$this->responseBody = is_string($result) ? $result : ''; |
74
|
|
|
|
75
|
|
|
$this->responseHttpCode = isset($info['http_code']) ? (int) $info['http_code'] : null; |
76
|
|
|
$this->responseContentType = $info['content_type'] ?? null; |
77
|
|
|
|
78
|
|
|
return true; |
79
|
|
|
} finally { |
80
|
|
|
curl_close($curl); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* If the httpCode is 200, return 200 |
86
|
|
|
* If the httpCode is 203, return 200 |
87
|
|
|
* If the httpCode is 404 ,return 404 |
88
|
|
|
* ... |
89
|
|
|
* |
90
|
|
|
* @return int |
91
|
|
|
*/ |
92
|
|
|
public function getHttpCodeType(): int |
93
|
|
|
{ |
94
|
|
|
return (int) (100 * floor($this->responseHttpCode / 100)); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|