honzamac / conditional-retry
Conditionally retry any third party api call
Installs: 13 845
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.0
Requires (Dev)
- phpunit/phpunit: ^6.2
This package is auto-updated.
Last update: 2025-07-24 11:19:12 UTC
README
Conditionally retry any third party api call.
Sometimes network connection is not working properly and we would like to retry api call. Using this tiny library you can easily retry any action based on specific error condition throwed by exception or returned value.
Usage
Reason for retry is returned via return $result
. First argument is required api call, second argument is conditional callback, third argument defines how many total runs will api call do.
<?php require __DIR__ . '/vendor/autoload.php'; retryConditional(function () { $result = $this->sms->send('+420800100200', 'Help, I\'m drowning!'); return $result; }, function ($returnValue) { return !$returnValue; }, 3);
Reason for retry is returned via RuntiomeException
.
<?php require __DIR__ . '/vendor/autoload.php'; retryConditional( function () { // do stuff throw new \RuntimeException(); }, function ($value, $exception) { return $exception instanceof \RuntimeException;}, 3 );
When all retries failed, last return value/exception is returned / re-throwed. For more information take a look on tests.
Requirements
- PHP 7.0 is required but using the latest version of PHP is highly recommended
Installation
You can add this library as a local, per-project dependency to your project using Composer:
composer require honzamac/conditional-retry
Contributing
Any contributions are welcome.