PHP code example of sparkcentral / backoff

1. Go to this page and download the library: Download sparkcentral/backoff 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/ */

    

sparkcentral / backoff example snippets




use Sparkcentral\Backoff\Backoff;

class ExternalServiceWrapper
{
    use Backoff;

    private $externalApiClient;

    public function __construct(ExternalApiClient $client)
    {
        $this->externalApiClient = $client;
    }

    public function getById($id)
    {
        $result = $this->backoffOnException(
            [$this->externalApiClient, 'get'], // call 'get' method on externalApiClient
            [$id], // pass this argument to 'get'
            5, // try up to 5 times
            [ConnectException::class] // only retry on ConnectExceptions, will re-throw everything else
        );

        return $result->getObject();
    }
}



use Sparkcentral\Backoff\Backoff;

class ExternalServiceWrapper
{
    use Backoff;

    private $externalApiClient;

    public function __construct(ExternalApiClient $client)
    {
        $this->externalApiClient = $client;
    }

    public function getById($id)
    {
        $result = $this->backoffOnCondition(
            [$this->externalApiClient, 'get'],
            [$id],
            5,
            function ($result) {
                return !is_null($result);
            }
        );

        return $result->getObject();
    }
}