11<?php
22namespace ScriptFUSION \Porter \Connector ;
33
4+ use Amp \Promise ;
45use ScriptFUSION \Porter \Connector \FetchExceptionHandler \FetchExceptionHandler ;
56use ScriptFUSION \Porter \Connector \FetchExceptionHandler \StatelessFetchExceptionHandler ;
67
@@ -39,50 +40,71 @@ public function __construct($mustCache, FetchExceptionHandler $fetchExceptionHan
3940 *
4041 * @return bool True if the response must be cached, otherwise false.
4142 */
42- public function mustCache ()
43+ public function mustCache (): bool
4344 {
4445 return $ this ->mustCache ;
4546 }
4647
4748 /**
48- * Retries the specified callback a predefined number of times with a predefined exception handler.
49+ * Retries the specified callback a predefined number of times with the specified resource fetch exception handler,
50+ * if set, and then the predefined fetch exception handler.
4951 *
5052 * @param callable $callback Callback.
5153 *
5254 * @return mixed The result of the callback invocation.
5355 */
5456 public function retry (callable $ callback )
5557 {
56- $ userHandlerCloned = $ providerHandlerCloned = false ;
57-
5858 return \ScriptFUSION \Retry \retry (
5959 $ this ->maxFetchAttempts ,
6060 $ callback ,
61- function (\Exception $ exception ) use (&$ userHandlerCloned , &$ providerHandlerCloned ) {
62- // Throw exception instead of retrying, if unrecoverable.
63- if (!$ exception instanceof RecoverableConnectorException) {
64- throw $ exception ;
65- }
66-
67- // Call provider's exception handler, if defined.
68- if ($ this ->resourceFetchExceptionHandler ) {
69- self ::invokeHandler ($ this ->resourceFetchExceptionHandler , $ exception , $ providerHandlerCloned );
70- }
71-
72- // Call user's exception handler.
73- self ::invokeHandler ($ this ->fetchExceptionHandler , $ exception , $ userHandlerCloned );
74- }
61+ $ this ->createExceptionHandler ()
7562 );
7663 }
7764
65+ /**
66+ * Retries the specified callback asynchronously a predefined number of times with the specified resource fetch
67+ * exception handler, if set, and then the predefined fetch exception handler.
68+ *
69+ * @param callable $callback
70+ */
71+ public function retryAsync (callable $ callback ): Promise
72+ {
73+ return \ScriptFUSION \Retry \retryAsync (
74+ $ this ->maxFetchAttempts ,
75+ $ callback ,
76+ $ this ->createExceptionHandler ()
77+ );
78+ }
79+
80+ private function createExceptionHandler (): \Closure
81+ {
82+ $ userHandlerCloned = $ providerHandlerCloned = false ;
83+
84+ return function (\Exception $ exception ) use (&$ userHandlerCloned , &$ providerHandlerCloned ) {
85+ // Throw exception instead of retrying, if unrecoverable.
86+ if (!$ exception instanceof RecoverableConnectorException) {
87+ throw $ exception ;
88+ }
89+
90+ // Call provider's exception handler, if defined.
91+ if ($ this ->resourceFetchExceptionHandler ) {
92+ self ::invokeHandler ($ this ->resourceFetchExceptionHandler , $ exception , $ providerHandlerCloned );
93+ }
94+
95+ // Call user's exception handler.
96+ self ::invokeHandler ($ this ->fetchExceptionHandler , $ exception , $ userHandlerCloned );
97+ };
98+ }
99+
78100 /**
79101 * Invokes the specified fetch exception handler, cloning it if required.
80102 *
81103 * @param FetchExceptionHandler $handler Fetch exception handler.
82104 * @param \Exception $exception Exception to pass to the handler.
83105 * @param bool $cloned False if handler requires cloning, true if handler has already been cloned.
84106 */
85- private static function invokeHandler (FetchExceptionHandler &$ handler , \Exception $ exception , &$ cloned )
107+ private static function invokeHandler (FetchExceptionHandler &$ handler , \Exception $ exception , &$ cloned ): void
86108 {
87109 if (!$ cloned && !$ handler instanceof StatelessFetchExceptionHandler) {
88110 $ handler = clone $ handler ;
@@ -100,7 +122,7 @@ private static function invokeHandler(FetchExceptionHandler &$handler, \Exceptio
100122 *
101123 * @param FetchExceptionHandler $resourceFetchExceptionHandler Exception handler.
102124 */
103- public function setResourceFetchExceptionHandler (FetchExceptionHandler $ resourceFetchExceptionHandler )
125+ public function setResourceFetchExceptionHandler (FetchExceptionHandler $ resourceFetchExceptionHandler ): void
104126 {
105127 if ($ this ->resourceFetchExceptionHandler !== null ) {
106128 throw new \LogicException ('Cannot set resource fetch exception handler: already set! ' );
0 commit comments