22
33namespace ApiClients \Middleware \Timer \Response ;
44
5- use ApiClients \Foundation \Middleware \ErrorTrait ;
5+ use ApiClients \Foundation \Middleware \Annotation \First ;
6+ use ApiClients \Foundation \Middleware \Annotation \Last ;
67use ApiClients \Foundation \Middleware \MiddlewareInterface ;
7- use ApiClients \Foundation \Middleware \Priority ;
88use Psr \Http \Message \RequestInterface ;
99use Psr \Http \Message \ResponseInterface ;
1010use React \Promise \CancellablePromiseInterface ;
11+ use Throwable ;
1112use function React \Promise \resolve ;
1213
1314final class Middleware implements MiddlewareInterface
1415{
15- use ErrorTrait;
16-
1716 const HEADER = 'X-Middleware-Timer-Response ' ;
1817
1918 /**
20- * @var float
19+ * @var float[]
2120 */
22- private $ time ;
21+ private $ times ;
2322
2423 /**
2524 * Return the processed $request via a fulfilled promise.
2625 * When implementing cache or other feature that returns a response, do it with a rejected promise.
2726 * If neither is possible, e.g. on some kind of failure, resolve the unaltered request.
2827 *
2928 * @param RequestInterface $request
29+ * @param string $transactionId
3030 * @param array $options
3131 * @return CancellablePromiseInterface
32+ *
33+ * @Last()
3234 */
33- public function pre (RequestInterface $ request , array $ options = []): CancellablePromiseInterface
34- {
35- $ this ->time = microtime (true );
35+ public function pre (
36+ RequestInterface $ request ,
37+ string $ transactionId ,
38+ array $ options = []
39+ ): CancellablePromiseInterface {
40+ $ this ->times [$ transactionId ] = microtime (true );
3641
3742 return resolve ($ request );
3843 }
@@ -41,24 +46,34 @@ public function pre(RequestInterface $request, array $options = []): Cancellable
4146 * Return the processed $response via a promise.
4247 *
4348 * @param ResponseInterface $response
49+ * @param string $transactionId
4450 * @param array $options
4551 * @return CancellablePromiseInterface
52+ *
53+ * @First()
4654 */
47- public function post (ResponseInterface $ response , array $ options = []): CancellablePromiseInterface
48- {
49- $ time = microtime (true ) - $ this ->time ;
55+ public function post (
56+ ResponseInterface $ response ,
57+ string $ transactionId ,
58+ array $ options = []
59+ ): CancellablePromiseInterface {
60+ $ time = microtime (true ) - $ this ->times [$ transactionId ];
61+ unset($ this ->times [$ transactionId ]);
5062
5163 return resolve ($ response ->withAddedHeader (self ::HEADER , (string )$ time ));
5264 }
5365
5466 /**
55- * Priority ranging from 0 to 1000. Where 1000 will be executed first on `pre` and 0 last on `pre`.
56- * For `post` the order is reversed.
57- *
58- * @return int
67+ * @param Throwable $throwable
68+ * @param string $transactionId
69+ * @param array $options
70+ * @return CancellablePromiseInterface
5971 */
60- public function priority (): int
61- {
62- return Priority::FIRST ;
72+ public function error (
73+ Throwable $ throwable ,
74+ string $ transactionId ,
75+ array $ options = []
76+ ): CancellablePromiseInterface {
77+ unset($ this ->times [$ transactionId ]);
6378 }
6479}
0 commit comments