1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Middleware\Skeleton; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Middleware\MiddlewareInterface; |
6
|
|
|
use Psr\Http\Message\RequestInterface; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use React\Promise\CancellablePromiseInterface; |
9
|
|
|
use Throwable; |
10
|
|
|
use function React\Promise\reject; |
11
|
|
|
use function React\Promise\resolve; |
12
|
|
|
|
13
|
|
|
final class Middleware implements MiddlewareInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Return the processed $request via a fulfilled promise. |
17
|
|
|
* When implementing cache or other feature that returns a response, do it with a rejected promise. |
18
|
|
|
* If neither is possible, e.g. on some kind of failure, resolve the unaltered request. |
19
|
|
|
* |
20
|
|
|
* @param RequestInterface $request |
21
|
|
|
* @param string $transactionId |
22
|
|
|
* @param array $options |
23
|
|
|
* @return CancellablePromiseInterface |
24
|
|
|
*/ |
25
|
2 |
|
public function pre( |
26
|
|
|
RequestInterface $request, |
27
|
|
|
string $transactionId, |
28
|
|
|
array $options = [] |
29
|
|
|
): CancellablePromiseInterface { |
30
|
|
|
// TODO: Implement pre() method or add PreTrait and remove this method |
31
|
2 |
|
return resolve($request); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Return the processed $response via a promise. |
36
|
|
|
* |
37
|
|
|
* @param ResponseInterface $response |
38
|
|
|
* @param string $transactionId |
39
|
|
|
* @param array $options |
40
|
|
|
* @return CancellablePromiseInterface |
41
|
|
|
*/ |
42
|
1 |
|
public function post( |
43
|
|
|
ResponseInterface $response, |
44
|
|
|
string $transactionId, |
45
|
|
|
array $options = [] |
46
|
|
|
): CancellablePromiseInterface { |
47
|
|
|
// TODO: Implement post() method. Or add PostTrait and remove this method |
48
|
1 |
|
return resolve($response); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Deal with possible errors that occurred during request/response events. |
53
|
|
|
* |
54
|
|
|
* @param Throwable $throwable |
55
|
|
|
* @param string $transactionId |
56
|
|
|
* @param array $options |
57
|
|
|
* @return CancellablePromiseInterface |
58
|
|
|
*/ |
59
|
1 |
|
public function error( |
60
|
|
|
Throwable $throwable, |
61
|
|
|
string $transactionId, |
62
|
|
|
array $options = [] |
63
|
|
|
): CancellablePromiseInterface { |
64
|
|
|
// TODO: Implement error() method. Or add ErrorTrait and remove this method |
65
|
1 |
|
return reject($throwable); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|