1 | <?php |
||
2 | |||
3 | namespace Jenky\Hermes\Middleware; |
||
4 | |||
5 | use GuzzleHttp\Exception\RequestException; |
||
6 | use Illuminate\Contracts\Events\Dispatcher; |
||
7 | use Jenky\Hermes\Events\RequestHandled; |
||
8 | use Psr\Http\Message\RequestInterface; |
||
9 | use Psr\Http\Message\ResponseInterface; |
||
10 | |||
11 | class RequestEvent |
||
12 | { |
||
13 | /** |
||
14 | * The event dispatcher instance. |
||
15 | * |
||
16 | * @var \Illuminate\Contracts\Events\Dispatcher|null |
||
17 | */ |
||
18 | protected $dispatcher; |
||
19 | |||
20 | /** |
||
21 | * Create a new handler instance. |
||
22 | * |
||
23 | * @param \Illuminate\Contracts\Events\Dispatcher|null $dispatcher |
||
24 | * @return void |
||
25 | */ |
||
26 | public function __construct(Dispatcher $dispatcher = null) |
||
27 | { |
||
28 | $this->dispatcher = $dispatcher; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Handle the request. |
||
33 | * |
||
34 | * @param callable $handler |
||
35 | * @return callable |
||
36 | */ |
||
37 | public function __invoke(callable $handler) |
||
38 | { |
||
39 | return function (RequestInterface $request, array $options) use ($handler) { |
||
40 | return $handler($request, $options)->then( |
||
41 | $this->handleSuccess($request, $options), |
||
42 | $this->handleFailure($request, $options) |
||
43 | ); |
||
44 | }; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Handler on fulfilled request. |
||
49 | * |
||
50 | * @param \Psr\Http\Message\RequestInterface $request |
||
51 | * @param array $options |
||
52 | * @return \Psr\Http\Message\ResponseInterface |
||
53 | */ |
||
54 | protected function handleSuccess(RequestInterface $request, array $options) |
||
55 | { |
||
56 | return function (ResponseInterface $response) use ($request, $options) { |
||
57 | $this->fireEvent($request, $response, $options); |
||
58 | |||
59 | return $response; |
||
60 | }; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Handler on rejected request. |
||
65 | * |
||
66 | * @param \Psr\Http\Message\RequestInterface $request |
||
67 | * @param array $options |
||
68 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
69 | */ |
||
70 | protected function handleFailure(RequestInterface $request, array $options) |
||
71 | { |
||
72 | return function ($reason) use ($request, $options) { |
||
73 | $response = $reason instanceof RequestException |
||
74 | ? $reason->getResponse() |
||
75 | : null; |
||
76 | |||
77 | $this->fireEvent($request, $response, $options); |
||
78 | |||
79 | return \GuzzleHttp\Promise\rejection_for($reason); |
||
0 ignored issues
–
show
|
|||
80 | }; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Get the event dispatcher instance. |
||
85 | * |
||
86 | * @return \Illuminate\Contracts\Events\Dispatcher |
||
87 | */ |
||
88 | public function getEventDispatcher() |
||
89 | { |
||
90 | return $this->dispatcher; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Set the event dispatcher instance. |
||
95 | * |
||
96 | * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher |
||
97 | * @return void |
||
98 | */ |
||
99 | public function setEventDispatcher(Dispatcher $dispatcher) |
||
100 | { |
||
101 | $this->dispatcher = $dispatcher; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Fires a request event. |
||
106 | * |
||
107 | * @param \Psr\Http\Message\RequestInterface $request |
||
108 | * @param \Psr\Http\Message\ResponseInterface|null $response |
||
109 | * @param array $options |
||
110 | * @return void |
||
111 | */ |
||
112 | protected function fireEvent(RequestInterface $request, ResponseInterface $response = null, array $options = []) |
||
113 | { |
||
114 | if ($this->dispatcher) { |
||
115 | $this->dispatcher->dispatch(new RequestHandled( |
||
116 | $request, $response, $options |
||
117 | )); |
||
118 | } |
||
119 | } |
||
120 | } |
||
121 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.