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