1 | <?php declare(strict_types = 1); |
||
2 | |||
3 | namespace SlevomatCsobGateway\Api\Driver; |
||
4 | |||
5 | use ErrorException; |
||
6 | use SlevomatCsobGateway\Api\ApiClientDriver; |
||
7 | use SlevomatCsobGateway\Api\HttpMethod; |
||
8 | use SlevomatCsobGateway\Api\Response; |
||
9 | use SlevomatCsobGateway\Api\ResponseCode; |
||
10 | use const CURLINFO_HEADER_SIZE; |
||
11 | use const CURLINFO_HTTP_CODE; |
||
12 | use const CURLOPT_COOKIESESSION; |
||
13 | use const CURLOPT_CUSTOMREQUEST; |
||
14 | use const CURLOPT_FOLLOWLOCATION; |
||
15 | use const CURLOPT_HEADER; |
||
16 | use const CURLOPT_HTTPHEADER; |
||
17 | use const CURLOPT_POSTFIELDS; |
||
18 | use const CURLOPT_RETURNTRANSFER; |
||
19 | use const CURLOPT_SSL_VERIFYPEER; |
||
20 | use const CURLOPT_TIMEOUT; |
||
21 | use function explode; |
||
22 | use function json_decode; |
||
23 | use function json_encode; |
||
24 | use function substr; |
||
25 | use function trim; |
||
26 | |||
27 | class CurlDriver implements ApiClientDriver |
||
28 | { |
||
29 | |||
30 | /** @var int */ |
||
31 | private $timeout = 20; |
||
32 | |||
33 | /** |
||
34 | * @param HttpMethod $method |
||
35 | * @param string $url |
||
36 | * @param mixed[]|null $data |
||
37 | * @param string[] $headers |
||
38 | * @return Response |
||
39 | * |
||
40 | * @throws CurlDriverException |
||
41 | */ |
||
42 | 2 | public function request(HttpMethod $method, string $url, ?array $data, array $headers = []): Response |
|
43 | { |
||
44 | 2 | $ch = curl_init($url); |
|
0 ignored issues
–
show
|
|||
45 | |||
46 | 2 | if ($ch === false) { |
|
0 ignored issues
–
show
|
|||
47 | throw new ErrorException('Failed to initialize curl resource.'); |
||
48 | } |
||
49 | |||
50 | 2 | if ($method->equalsValue(HttpMethod::POST) || $method->equalsValue(HttpMethod::PUT)) { |
|
51 | 2 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method->getValue()); |
|
52 | 2 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); |
|
53 | } |
||
54 | |||
55 | 2 | curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); |
|
56 | 2 | curl_setopt($ch, CURLOPT_COOKIESESSION, true); |
|
57 | 2 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
58 | 2 | curl_setopt($ch, CURLOPT_HEADER, true); |
|
59 | 2 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
|
60 | 2 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers + [ |
|
61 | 2 | 'Content-Type: application/json', |
|
62 | ]); |
||
63 | 2 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); |
|
64 | 2 | $output = curl_exec($ch); |
|
65 | |||
66 | 2 | if ($output === false) { |
|
67 | 1 | throw new CurlDriverException($ch); |
|
68 | } |
||
69 | |||
70 | 1 | $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
|
71 | 1 | $headers = substr((string) $output, 0, $headerSize); |
|
72 | 1 | $body = substr((string) $output, $headerSize); |
|
73 | |||
74 | 1 | $responseCode = ResponseCode::get(curl_getinfo($ch, CURLINFO_HTTP_CODE)); |
|
75 | |||
76 | 1 | curl_close($ch); |
|
77 | |||
78 | 1 | return new Response( |
|
79 | 1 | $responseCode, |
|
80 | 1 | json_decode($body, true), |
|
81 | 1 | $this->parseHeaders($headers) |
|
82 | ); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param string $rawHeaders |
||
87 | * @return string[] |
||
88 | */ |
||
89 | 1 | private function parseHeaders(string $rawHeaders): array |
|
90 | { |
||
91 | 1 | $headers = []; |
|
92 | |||
93 | 1 | foreach (explode("\n", $rawHeaders) as $line) { |
|
94 | 1 | $line = explode(':', $line, 2); |
|
95 | |||
96 | 1 | if (!isset($line[1])) { |
|
97 | continue; |
||
98 | } |
||
99 | |||
100 | 1 | $headers[$line[0]] = trim($line[1]); |
|
101 | } |
||
102 | |||
103 | 1 | return $headers; |
|
104 | } |
||
105 | |||
106 | } |
||
107 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.