Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
49 lines (30 sloc) 1.34 KB

Operations

An operation is an atomic piece of processing. This is for instance an API call to an third-party service, or a process that requires to talk to the database. We can use them for any process that is dependent on a non-trusted resource, starting with the network connection.

From a callback

The first kind of operation is an operation defined by a PHP callable. This operation can be created with the Callback class, like this:

use Tolerance\Operation\Callback;

$operation = new Callback(function() use ($client) {
    return $client->get('/foo');
});

This class accepts any supported PHP callable, so you can also use object methods. For instance:

use Tolerance\Operation\Callback;

$operation = new Callback([$this, 'run']);

Promise Operation

This class accepts any supported PHP callable, which must returns a Promise.

For instance:

use Tolerance\Operation\PromiseOperation;

$operation = new PromiseOperation(function () use ($nextHandler, $request) {
    return $nextHandler($request);
});

The PromiseOperation is runned by the RetryPromiseOperationRunner.