1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Middleware\Debug; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Middleware\MiddlewareInterface; |
6
|
|
|
use Psr\Http\Message\RequestInterface; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use React\EventLoop\LoopInterface; |
9
|
|
|
use React\Promise\CancellablePromiseInterface; |
10
|
|
|
use React\Stream\WritableResourceStream; |
11
|
|
|
use React\Stream\WritableStreamInterface; |
12
|
|
|
use Throwable; |
13
|
|
|
use function React\Promise\reject; |
14
|
|
|
use function React\Promise\resolve; |
15
|
|
|
|
16
|
|
|
final class DebugMiddleware implements MiddlewareInterface |
17
|
|
|
{ |
18
|
|
|
const HR = '---------------------------------------' . PHP_EOL; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var WritableStreamInterface |
22
|
|
|
*/ |
23
|
|
|
private $stdout; |
24
|
|
|
|
25
|
1 |
|
public function __construct(LoopInterface $loop, WritableStreamInterface $stdout = null) |
26
|
|
|
{ |
27
|
1 |
|
if ($stdout === null) { |
28
|
|
|
$stdout = new WritableResourceStream(STDOUT, $loop); |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
$this->stdout = $stdout; |
32
|
1 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Return the processed $request via a fulfilled promise. |
36
|
|
|
* When implementing cache or other feature that returns a response, do it with a rejected promise. |
37
|
|
|
* If neither is possible, e.g. on some kind of failure, resolve the unaltered request. |
38
|
|
|
* |
39
|
|
|
* @param RequestInterface $request |
40
|
|
|
* @param string $transactionId |
41
|
|
|
* @param array $options |
42
|
|
|
* @return CancellablePromiseInterface |
43
|
|
|
*/ |
44
|
1 |
|
public function pre(RequestInterface $request, string $transactionId, array $options = []): CancellablePromiseInterface |
45
|
|
|
{ |
46
|
1 |
|
$this->stdout->write(self::HR); |
47
|
1 |
|
$this->stdout->write($transactionId . ': request' . PHP_EOL); |
48
|
1 |
|
$this->stdout->write(self::HR); |
49
|
1 |
|
$this->stdout->write('Method: ' . $request->getMethod() . PHP_EOL); |
50
|
1 |
|
$this->stdout->write('URI: ' . (string)$request->getUri() . PHP_EOL); |
51
|
1 |
|
$this->stdout->write(self::HR); |
52
|
|
|
|
53
|
1 |
|
return resolve($request); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return the processed $response via a promise. |
58
|
|
|
* |
59
|
|
|
* @param ResponseInterface $response |
60
|
|
|
* @param string $transactionId |
61
|
|
|
* @param array $options |
62
|
|
|
* @return CancellablePromiseInterface |
63
|
|
|
*/ |
64
|
1 |
View Code Duplication |
public function post(ResponseInterface $response, string $transactionId, array $options = []): CancellablePromiseInterface |
|
|
|
|
65
|
|
|
{ |
66
|
1 |
|
$this->stdout->write(self::HR); |
67
|
1 |
|
$this->stdout->write($transactionId . ': response' . PHP_EOL); |
68
|
1 |
|
$this->stdout->write(self::HR); |
69
|
1 |
|
$this->stdout->write('Status: ' . $response->getStatusCode() . PHP_EOL); |
70
|
1 |
|
$this->stdout->write(self::HR); |
71
|
|
|
|
72
|
1 |
|
return resolve($response); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Deal with possible errors that occurred during request/response events. |
77
|
|
|
* |
78
|
|
|
* @param Throwable $throwable |
79
|
|
|
* @param string $transactionId |
80
|
|
|
* @param array $options |
81
|
|
|
* @return CancellablePromiseInterface |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function error(Throwable $throwable, string $transactionId, array $options = []): CancellablePromiseInterface |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$this->stdout->write(self::HR); |
86
|
|
|
$this->stdout->write($transactionId . ': error' . PHP_EOL); |
87
|
|
|
$this->stdout->write(self::HR); |
88
|
|
|
$this->stdout->write((string)$throwable); |
89
|
|
|
$this->stdout->write(self::HR); |
90
|
|
|
|
91
|
|
|
return reject($throwable); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.