1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace ApiClients\Foundation\Transport; |
5
|
|
|
|
6
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
7
|
|
|
use GuzzleHttp\Psr7\Request; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use React\Cache\CacheInterface; |
11
|
|
|
use React\EventLoop\LoopInterface; |
12
|
|
|
use React\Promise\Deferred; |
13
|
|
|
use React\Promise\PromiseInterface; |
14
|
|
|
use function React\Promise\reject; |
15
|
|
|
use function React\Promise\resolve; |
16
|
|
|
use function WyriHaximus\React\futureFunctionPromise; |
17
|
|
|
|
18
|
|
|
class Client |
19
|
|
|
{ |
20
|
|
|
const DEFAULT_OPTIONS = [ |
21
|
|
|
'schema' => 'https', |
22
|
|
|
'path' => '/', |
23
|
|
|
'user_agent' => 'WyriHaximus/php-api-client', |
24
|
|
|
'headers' => [], |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var GuzzleClient |
29
|
|
|
*/ |
30
|
|
|
protected $handler; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var LoopInterface |
34
|
|
|
*/ |
35
|
|
|
protected $loop; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $options = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Hydrator |
44
|
|
|
*/ |
45
|
|
|
protected $hydrator; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var CacheInterface |
49
|
|
|
*/ |
50
|
|
|
protected $cache; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param LoopInterface $loop |
54
|
|
|
* @param GuzzleClient $handler |
55
|
|
|
* @param array $options |
56
|
|
|
*/ |
57
|
12 |
|
public function __construct(LoopInterface $loop, GuzzleClient $handler, array $options = []) |
58
|
|
|
{ |
59
|
12 |
|
$this->loop = $loop; |
60
|
12 |
|
$this->handler = $handler; |
61
|
12 |
|
$this->options = $options + self::DEFAULT_OPTIONS; |
62
|
12 |
|
if (isset($this->options['cache']) && $this->options['cache'] instanceof CacheInterface) { |
63
|
3 |
|
$this->cache = $this->options['cache']; |
64
|
|
|
} |
65
|
12 |
|
$this->hydrator = new Hydrator($this, $options); |
66
|
12 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $path |
70
|
|
|
* @param bool $refresh |
71
|
|
|
* @return PromiseInterface |
72
|
|
|
*/ |
73
|
5 |
|
public function request(string $path, bool $refresh = false): PromiseInterface |
74
|
|
|
{ |
75
|
|
|
return $this->requestRaw($path, $refresh)->then(function ($json) { |
76
|
3 |
|
return $this->jsonDecode($json); |
77
|
5 |
|
}); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $path |
82
|
|
|
* @param bool $refresh |
83
|
|
|
* @return PromiseInterface |
84
|
|
|
*/ |
85
|
5 |
|
public function requestRaw(string $path, bool $refresh = false): PromiseInterface |
86
|
|
|
{ |
87
|
5 |
|
if ($refresh) { |
88
|
2 |
|
return $this->sendRequest($path); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->checkCache($path)->otherwise(function () use ($path) { |
|
|
|
|
92
|
2 |
|
return $this->sendRequest($path); |
93
|
3 |
|
}); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $path |
98
|
|
|
* @return PromiseInterface |
99
|
|
|
*/ |
100
|
3 |
|
protected function checkCache(string $path): PromiseInterface |
101
|
|
|
{ |
102
|
3 |
|
if ($this->cache instanceof CacheInterface) { |
103
|
2 |
|
return $this->cache->get($path); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return reject(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $path |
111
|
|
|
* @param string $method |
112
|
|
|
* @param bool $raw |
113
|
|
|
* @return PromiseInterface |
114
|
|
|
*/ |
115
|
4 |
|
protected function sendRequest(string $path, string $method = 'GET', bool $raw = false): PromiseInterface |
116
|
|
|
{ |
117
|
4 |
|
return $this->requestPsr7( |
118
|
4 |
|
$this->createRequest($method, $path) |
119
|
|
|
)->then(function ($response) use ($path, $raw) { |
120
|
2 |
|
$json = $response->getBody()->getContents(); |
121
|
|
|
|
122
|
2 |
|
if ($this->cache instanceof CacheInterface) { |
123
|
2 |
|
$this->cache->set($path, $json); |
124
|
|
|
} |
125
|
|
|
|
126
|
2 |
|
return resolve($json); |
127
|
4 |
|
}); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param RequestInterface $request |
132
|
|
|
* @return PromiseInterface |
133
|
|
|
*/ |
134
|
4 |
|
public function requestPsr7(RequestInterface $request): PromiseInterface |
135
|
|
|
{ |
136
|
4 |
|
$deferred = new Deferred(); |
137
|
|
|
|
138
|
4 |
|
$this->handler->sendAsync( |
139
|
|
|
$request |
140
|
|
|
)->then(function (ResponseInterface $response) use ($deferred) { |
141
|
2 |
|
$deferred->resolve($response); |
142
|
|
|
}, function ($error) use ($deferred) { |
143
|
|
|
$deferred->reject($error); |
144
|
4 |
|
}); |
145
|
|
|
|
146
|
4 |
|
return $deferred->promise(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $method |
151
|
|
|
* @param string $path |
152
|
|
|
* @return RequestInterface |
153
|
|
|
*/ |
154
|
4 |
|
protected function createRequest(string $method, string $path): RequestInterface |
155
|
|
|
{ |
156
|
4 |
|
$url = $this->getBaseURL() . $path; |
157
|
4 |
|
$headers = $this->getHeaders(); |
158
|
4 |
|
return new Request($method, $url, $headers); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
4 |
|
public function getHeaders(): array |
165
|
|
|
{ |
166
|
|
|
$headers = [ |
167
|
4 |
|
'User-Agent' => $this->options['user_agent'], |
168
|
|
|
]; |
169
|
4 |
|
$headers += $this->options['headers']; |
170
|
4 |
|
return $headers; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $json |
175
|
|
|
* @return PromiseInterface |
176
|
|
|
*/ |
177
|
|
|
public function jsonDecode(string $json): PromiseInterface |
178
|
|
|
{ |
179
|
3 |
|
return futureFunctionPromise($this->loop, $json, function ($json) { |
180
|
3 |
|
return json_decode($json, true); |
181
|
3 |
|
}); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return Hydrator |
186
|
|
|
*/ |
187
|
1 |
|
public function getHydrator(): Hydrator |
188
|
|
|
{ |
189
|
1 |
|
return $this->hydrator; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return LoopInterface |
194
|
|
|
*/ |
195
|
3 |
|
public function getLoop(): LoopInterface |
196
|
|
|
{ |
197
|
3 |
|
return $this->loop; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
7 |
|
public function getBaseURL(): string |
204
|
|
|
{ |
205
|
7 |
|
return $this->options['schema'] . '://' . $this->options['host'] . $this->options['path']; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: