1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Cerbero\LazyJsonPages\Middleware; |
||
6 | |||
7 | use Cerbero\LazyJsonPages\Services\TapCallbacks; |
||
8 | use Closure; |
||
9 | use GuzzleHttp\Exception\RequestException; |
||
10 | use GuzzleHttp\Promise\Create; |
||
11 | use GuzzleHttp\Promise\PromiseInterface; |
||
12 | use Psr\Http\Message\RequestInterface; |
||
13 | use Psr\Http\Message\ResponseInterface; |
||
14 | |||
15 | /** |
||
16 | * The middleware to handle an HTTP request before and after it is sent. |
||
17 | */ |
||
18 | final class Tap |
||
19 | { |
||
20 | /** |
||
21 | * The HTTP request. |
||
22 | */ |
||
23 | private RequestInterface $request; |
||
24 | |||
25 | /** |
||
26 | * The Guzzle client configuration. |
||
27 | * |
||
28 | * @var array<string, mixed> |
||
29 | */ |
||
30 | private array $config; |
||
31 | |||
32 | /** |
||
33 | * Instantiate the class statically to tap once. |
||
34 | */ |
||
35 | 3 | public static function once(?Closure $onRequest = null, ?Closure $onResponse = null, ?Closure $onError = null): self |
|
36 | { |
||
37 | 3 | $callbacks = new TapCallbacks(); |
|
38 | 3 | $onRequest && $callbacks->onRequest($onRequest); |
|
39 | 3 | $onResponse && $callbacks->onResponse($onResponse); |
|
40 | 3 | $onError && $callbacks->onError($onError); |
|
41 | |||
42 | 3 | return new self($callbacks); |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * Instantiate the class. |
||
47 | */ |
||
48 | 6 | public function __construct(private readonly TapCallbacks $callbacks) {} |
|
49 | |||
50 | /** |
||
51 | * Handle an HTTP request before and after it is sent. |
||
52 | * |
||
53 | * @param callable(RequestInterface, array<string, mixed>): PromiseInterface $handler |
||
54 | */ |
||
55 | 6 | public function __invoke(callable $handler): Closure |
|
56 | { |
||
57 | 6 | return function (RequestInterface $request, array $config) use ($handler) { |
|
58 | 6 | $this->request = $request; |
|
59 | 6 | $this->config = $config; |
|
60 | |||
61 | 6 | foreach ($this->callbacks->onRequestCallbacks() as $callback) { |
|
62 | 5 | $callback($request, $config); |
|
63 | } |
||
64 | |||
65 | 6 | return $handler($request, $config) |
|
66 | 6 | ->then($this->handleResponse(...)) |
|
67 | 6 | ->otherwise($this->handleError(...)); |
|
68 | 6 | }; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Handle the given response. |
||
73 | */ |
||
74 | 4 | private function handleResponse(ResponseInterface $response): ResponseInterface |
|
0 ignored issues
–
show
|
|||
75 | { |
||
76 | 4 | foreach ($this->callbacks->onResponseCallbacks() as $callback) { |
|
77 | 3 | $callback($response, $this->request, $this->config); |
|
78 | } |
||
79 | |||
80 | 4 | return $response; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Handle the given transaction error. |
||
85 | */ |
||
86 | 2 | private function handleError(mixed $reason): PromiseInterface |
|
87 | { |
||
88 | 2 | $exception = Create::exceptionFor($reason); |
|
89 | 2 | $response = $reason instanceof RequestException ? $reason->getResponse() : null; |
|
90 | |||
91 | 2 | foreach ($this->callbacks->onErrorCallbacks() as $callback) { |
|
92 | 2 | $callback($exception, $this->request, $response, $this->config); |
|
93 | } |
||
94 | |||
95 | 2 | return Create::rejectionFor($reason); |
|
96 | } |
||
97 | } |
||
98 |
This check looks for private methods that have been defined, but are not used inside the class.