diff --git a/lib/RetryMiddlewareFactory.php b/lib/RetryMiddlewareFactory.php index 5df606d4..f063d475 100644 --- a/lib/RetryMiddlewareFactory.php +++ b/lib/RetryMiddlewareFactory.php @@ -8,34 +8,24 @@ class RetryMiddlewareFactory { - private const DEFAULT_MAX_RETRIES = 5; - private const INTERNAL_ERROR_RANGES = [ - [500, 503], - [520, 599] - ]; - public static function createInternalErrorsMiddleware( ?callable $delayFunction = null, - int $maxRetries = self::DEFAULT_MAX_RETRIES + int $maxRetries = 5 ) { - return static::createMiddlewareByHttpCodeRanges( - self::INTERNAL_ERROR_RANGES, - $delayFunction, - $maxRetries - ); + return static::createMiddlewareByHttpCodeRange(500, 504, $delayFunction, $maxRetries); } public static function createRateLimitMiddleware( ?callable $delayFunction = null, - int $maxRetries = self::DEFAULT_MAX_RETRIES + int $maxRetries = 5 ) { return static::createMiddlewareByHttpCodes([429], $delayFunction, $maxRetries); } public static function createMiddlewareByHttpCodes( array $codes, - ?callable $delayFunction, - int $maxRetries = self::DEFAULT_MAX_RETRIES + callable $delayFunction, + int $maxRetries = 5 ): callable { return Middleware::retry( static::getRetryFunction($codes, $maxRetries), @@ -46,43 +36,31 @@ public static function createMiddlewareByHttpCodes( public static function createMiddlewareByHttpCodeRange( int $from, int $to, - ?callable $delayFunction, - int $maxRetries = self::DEFAULT_MAX_RETRIES - ): callable { - return static::createMiddlewareByHttpCodeRanges([[$from, $to]], $delayFunction, $maxRetries); - } - - private static function createMiddlewareByHttpCodeRanges( - array $ranges, - ?callable $delayFunction, - int $maxRetries = self::DEFAULT_MAX_RETRIES + callable $delayFunction, + int $maxRetries = 5 ): callable { return Middleware::retry( - static::getRetryFunctionByRanges($ranges, $maxRetries), + static::getRetryFunctionByRange($from, $to, $maxRetries), $delayFunction ); } - private static function getRetryFunctionByRanges( - array $ranges, - int $maxRetries - ): callable { + public static function getRetryFunctionByRange( + int $from, + int $to, + int $maxRetries = 5 + ) { return function ( $retries, Request $request, ?Response $response = null - ) use ($ranges, $maxRetries) { + ) use ($from, $to, $maxRetries) { if ($retries >= $maxRetries) { return false; } - if (!$response instanceof Response) { - return false; - } - - $statusCode = $response->getStatusCode(); - foreach ($ranges as $range) { - if ($statusCode >= $range[0] && $statusCode <= $range[1]) { + if ($response instanceof Response) { + if (($response->getStatusCode() >= $from) && ($response->getStatusCode() <= $to)) { return true; } } @@ -91,18 +69,8 @@ private static function getRetryFunctionByRanges( }; } - public static function getRetryFunctionByRange( - int $from, - int $to, - int $maxRetries = self::DEFAULT_MAX_RETRIES - ): callable { - return static::getRetryFunctionByRanges([[$from, $to]], $maxRetries); - } - - public static function getRetryFunction( - array $codes, - int $maxRetries = self::DEFAULT_MAX_RETRIES - ): callable { + public static function getRetryFunction(array $codes, int $maxRetries = 5) + { return function ( $retries, Request $request,