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:

1
2
3
4
5
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:

1
2
3
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:

1
2
3
4
5
use Tolerance\Operation\PromiseOperation;

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

The PromiseOperation is runned by the RetryPromiseOperationRunner.