PHP code example of s-patompong / php-retrier

1. Go to this page and download the library: Download s-patompong/php-retrier library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

s-patompong / php-retrier example snippets




// Your own API class
use App\Api\ApiConnector;
use SPatompong\Retrier\Retrier;

$api = new ApiConnector();

// By default, Retrier use RetryThrowableStrategy which will retry if the result is an instance of \Throwable
$result = (new Retrier())
    ->setLogic(function() use($api) {
        return $api->get();
    })
    ->execute();



// Your own API class
use App\Api\ApiConnector;
use SPatompong\Retrier\Retrier;

$api = new ApiConnector();

$retrier = (new Retrier())
    ->setLogic(function() {
        return 0;
    });

// After 3 retries, it's possible that the code still gets the \Throwable
// Thus, we still need to put it in a try/catch block
try {
    $value = $retrier->execute();
} catch(\Throwable $t) {
    echo "Still gets throwable after 3 retries.\n";
}



// Your own API class
use App\Api\ApiConnector;
use SPatompong\Retrier\Retrier;
use SPatompong\Retrier\Presets\Strategies\RetryNullStrategy;

$api = new ApiConnector();

// Keep track of retry count, useful for logging or echoing to the terminal
$retryCount = 0;

$value = (new Retrier())
    // Change the stragegy to RetryNullStrategy to keep retrying if the Logic returns null
    ->setRetryStrategy(new RetryNullStrategy())
    
    // Set the wait time for each retry to 10 seconds
    ->setDelay(10)
    
    // Let the code retry 5 times
    ->setRetryTimes(5)
    
    // Set the onRetryListener to print out some useful log
    ->setOnRetryListener(function ($currentTryCount, $value, $throwable) use (&$retryCount) {
        $retryCount++;
        echo "Failed to get API data, retry count: $retryCount\n";
    })
    
    // Set the logic
    ->setLogic(fn () => $api->get())
    
    // Execute it
    ->execute();
    
// At this point, value could still be null if after 5 times the code still couldn't get the API data
echo $value;



use SPatompong\Retrier\Retrier;
use SPatompong\Retrier\Tests\Helpers\FakeClass;

$fakeClass = new FakeClass();

$publicMethodResult = (new Retrier())
    ->setLogic([$fakeClass, 'fakePublicMethod'])
    ->execute();
    
$staticMethodResult = (new Retrier())
    ->setLogic([FakeClass::class, 'fakeStaticMethod'])
    ->execute();



namespace SPatompong\Retrier\Contracts;

interface RetryStrategy
{
    /**
     * Add a logic to check if the retrier should retry
     *
     * @param mixed $value
     * @return bool
     */
    public function shouldRetry(mixed $value): bool;
}



namespace App\RetryStrategies;

use SPatompong\Retrier\Contracts\RetryStrategy;
use GuzzleHttp\Exception\ClientException;

class RetryGuzzleClientExceptionStrategy implements RetryStrategy
{
    public function shouldRetry(mixed $value): bool
    {
        return $value instanceof ClientException;
    }
}



use SPatompong\Retrier\Retrier;
use App\RetryStrategies\RetryGuzzleClientExceptionStrategy;

$retrier = (new Retrier())
    ->setRetryStrategy(new RetryGuzzleClientExceptionStrategy())
    
try {
    $retrier->execute();
} catch(\Throwable $t) {
    // Still gets ClientException after retry or other type of Throwable
}