|
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->requestHeaders = []; |
|
54
|
|
|
// Default Content-Type for |
|
55
|
|
|
$this->requestContentType = 'application/x-www-form-urlencoded'; |
|
56
|
|
|
$this->response = ''; |
|
57
|
|
|
$this->timeout = 60; |
|
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
|
|
|
public function setDebug(bool $debug): bool |
|
101
|
|
|
{ |
|
102
|
|
|
$this->debug = $debug; |
|
103
|
|
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function setMethod(string $method): bool |
|
107
|
|
|
{ |
|
108
|
|
|
if (!\in_array($method, Method::getSupported(), true)) { |
|
109
|
|
|
throw new HttpClientException('Unsupported method.'); |
|
110
|
|
|
} |
|
111
|
|
|
$this->method = $method; |
|
112
|
|
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param array<string,mixed>|string $data |
|
117
|
|
|
*/ |
|
118
|
|
|
public function setRequestData($data): bool |
|
119
|
|
|
{ |
|
120
|
|
|
if (\is_array($data)) { |
|
121
|
|
|
$this->requestData = []; |
|
122
|
|
|
foreach ($data as $key => $value) { |
|
123
|
|
|
if (\is_array($value)) { |
|
124
|
|
|
throw new \InvalidArgumentException('Request data value can not be an array.'); |
|
125
|
|
|
} |
|
126
|
|
|
$this->requestData[$key] = $value; |
|
127
|
|
|
} |
|
128
|
|
|
return true; |
|
129
|
|
|
} |
|
130
|
|
|
$this->requestData = $data; |
|
131
|
|
|
return true; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function setRequestContentType(string $contentType): bool |
|
135
|
|
|
{ |
|
136
|
|
|
$this->requestContentType = $contentType; |
|
137
|
|
|
return true; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function setRequestHeader(string $name, string $value): bool |
|
141
|
|
|
{ |
|
142
|
|
|
$this->requestHeaders[$name] = $value; |
|
143
|
|
|
return true; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function setSkipSSlVerification(bool $skipSslVerification): bool |
|
147
|
|
|
{ |
|
148
|
|
|
$this->skipSslVerification = $skipSslVerification; |
|
149
|
|
|
return true; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function setTimeout(int $timeout): bool |
|
153
|
|
|
{ |
|
154
|
|
|
$this->timeout = $timeout; |
|
155
|
|
|
return true; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|