strategy = $strategy; $this->retries = $retries; } /** * The number of retries to perform in case of failure * * @param callable $action The callable action to execute with a retry strategy * @return mixed The return value of the passed action callable * @throws \Exception */ public function run(callable $action) { $retryCount = 0; $lastException = null; do { try { return $action(); } catch (Exception $e) { $lastException = $e; if (!$this->strategy->shouldRetry($e, $retryCount)) { throw $e; } } } while ($this->retries > $retryCount++); if ($lastException !== null) { throw $lastException; } } }