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 array $options |
22
|
|
|
* @return CancellablePromiseInterface |
23
|
|
|
*/ |
24
|
2 |
|
public function pre( |
25
|
|
|
RequestInterface $request, |
26
|
|
|
string $transactionId, |
27
|
|
|
array $options = [] |
28
|
|
|
): CancellablePromiseInterface { |
29
|
|
|
// TODO: Implement pre() method or add PreTrait and remove this method |
30
|
2 |
|
return resolve($request); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Return the processed $response via a promise. |
35
|
|
|
* |
36
|
|
|
* @param ResponseInterface $response |
37
|
|
|
* @param array $options |
38
|
|
|
* @return CancellablePromiseInterface |
39
|
|
|
*/ |
40
|
1 |
|
public function post( |
41
|
|
|
ResponseInterface $response, |
42
|
|
|
string $transactionId, |
43
|
|
|
array $options = [] |
44
|
|
|
): CancellablePromiseInterface { |
45
|
|
|
// TODO: Implement post() method. Or add PostTrait and remove this method |
46
|
1 |
|
return resolve($response); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Deal with possible errors that occurred during request/response events. |
51
|
|
|
* |
52
|
|
|
* @param Throwable $throwable |
53
|
|
|
* @param array $options |
54
|
|
|
* @return CancellablePromiseInterface |
55
|
|
|
*/ |
56
|
1 |
|
public function error( |
57
|
|
|
Throwable $throwable, |
58
|
|
|
string $transactionId, |
59
|
|
|
array $options = [] |
60
|
|
|
): CancellablePromiseInterface { |
61
|
|
|
// TODO: Implement error() method. Or add ErrorTrait and remove this method |
62
|
1 |
|
return reject($throwable); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|