1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WebServCo\Framework\Http; |
6
|
|
|
|
7
|
|
|
use WebServCo\Framework\Exceptions\HttpClientException; |
8
|
|
|
|
9
|
|
|
abstract class AbstractClient |
10
|
|
|
{ |
11
|
|
|
protected bool $debug; |
12
|
|
|
|
13
|
|
|
protected \WebServCo\Framework\Interfaces\LoggerInterface $logger; |
14
|
|
|
|
15
|
|
|
protected string $method; |
16
|
|
|
|
17
|
|
|
protected ?string $requestContentType; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Request data |
21
|
|
|
* |
22
|
|
|
* @var array<string,string>|string |
23
|
|
|
*/ |
24
|
|
|
protected $requestData; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Request headers |
28
|
|
|
* |
29
|
|
|
* @var array<string,mixed> |
30
|
|
|
*/ |
31
|
|
|
protected array $requestHeaders; |
32
|
|
|
|
33
|
|
|
protected string $response; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Resposne headers |
37
|
|
|
* |
38
|
|
|
* @var array<int,array<string,mixed>> |
39
|
|
|
*/ |
40
|
|
|
protected array $responseHeaders; |
41
|
|
|
|
42
|
|
|
protected bool $skipSslVerification; |
43
|
|
|
|
44
|
|
|
protected int $timeout; |
45
|
|
|
|
46
|
|
|
abstract public function retrieve(string $url): Response; |
47
|
|
|
|
48
|
|
|
public function __construct(\WebServCo\Framework\Interfaces\LoggerInterface $logger) |
49
|
|
|
{ |
50
|
|
|
$this->logger = $logger; |
51
|
|
|
$this->debug = false; |
52
|
|
|
$this->skipSslVerification = false; |
53
|
|
|
$this->response = ''; |
54
|
|
|
$this->timeout = 60; |
55
|
|
|
|
56
|
|
|
// Reset headers and data. |
57
|
|
|
$this->reset(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function get(string $url): Response |
61
|
|
|
{ |
62
|
|
|
$this->setMethod(Method::GET); |
63
|
|
|
return $this->retrieve($url); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return array<string,string> |
68
|
|
|
*/ |
69
|
|
|
public function getRequestHeaders(): array |
70
|
|
|
{ |
71
|
|
|
return $this->requestHeaders; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return array<int,array<string,mixed>> |
76
|
|
|
*/ |
77
|
|
|
public function getResponseHeaders(): array |
78
|
|
|
{ |
79
|
|
|
return $this->responseHeaders; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function head(string $url): Response |
83
|
|
|
{ |
84
|
|
|
$this->setMethod(Method::HEAD); |
85
|
|
|
return $this->retrieve($url); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array<string,mixed>|string $data |
90
|
|
|
*/ |
91
|
|
|
public function post(string $url, $data = null): Response |
92
|
|
|
{ |
93
|
|
|
$this->setMethod(Method::POST); |
94
|
|
|
if ($data) { |
95
|
|
|
$this->setRequestData($data); |
96
|
|
|
} |
97
|
|
|
return $this->retrieve($url); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Reset headers and data. |
102
|
|
|
* |
103
|
|
|
* Needs to be called if running multiple times in a row. |
104
|
|
|
*/ |
105
|
|
|
public function reset(): bool |
106
|
|
|
{ |
107
|
|
|
$this->requestContentType = null; |
108
|
|
|
$this->requestHeaders = []; |
109
|
|
|
$this->requestData = []; |
110
|
|
|
|
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function setDebug(bool $debug): bool |
115
|
|
|
{ |
116
|
|
|
$this->debug = $debug; |
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function setMethod(string $method): bool |
121
|
|
|
{ |
122
|
|
|
if (!\in_array($method, Method::getSupported(), true)) { |
123
|
|
|
throw new HttpClientException('Unsupported method.'); |
124
|
|
|
} |
125
|
|
|
$this->method = $method; |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param array<string,mixed>|string $data |
131
|
|
|
*/ |
132
|
|
|
public function setRequestData($data): bool |
133
|
|
|
{ |
134
|
|
|
if (\is_array($data)) { |
135
|
|
|
$this->requestData = []; |
136
|
|
|
foreach ($data as $key => $value) { |
137
|
|
|
if (\is_array($value)) { |
138
|
|
|
throw new \InvalidArgumentException('Request data value can not be an array.'); |
139
|
|
|
} |
140
|
|
|
$this->requestData[$key] = $value; |
141
|
|
|
} |
142
|
|
|
return true; |
143
|
|
|
} |
144
|
|
|
$this->requestData = $data; |
145
|
|
|
return true; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function setRequestContentType(string $contentType): bool |
149
|
|
|
{ |
150
|
|
|
$this->requestContentType = $contentType; |
151
|
|
|
return true; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function setRequestHeader(string $name, string $value): bool |
155
|
|
|
{ |
156
|
|
|
$this->requestHeaders[$name] = $value; |
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function setSkipSSlVerification(bool $skipSslVerification): bool |
161
|
|
|
{ |
162
|
|
|
$this->skipSslVerification = $skipSslVerification; |
163
|
|
|
return true; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function setTimeout(int $timeout): bool |
167
|
|
|
{ |
168
|
|
|
$this->timeout = $timeout; |
169
|
|
|
return true; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|