Circuit Breaker is a service that gives you the control of your external dependencies
Require the library as usual:
composer require "pluggit/circuit-breaker"
In order for this library to work you need to install a cache library, you can use any PSR-16 simple cache but we encourage you to use the pluggit one. You can do so requiring it via composer:
composer require "pluggit/cache"
<?php
$circuitBreaker = new \Cmp\CircuitBreaker\CircuitBreaker($mySimpleCache);
$circuitBreaker->trackService(new \Cmp\CircuitBreaker\Service('payment.gateway'));
...
if ( $circuitBreaker->isAvailable('payment.gateway') ) {
try {
$paymentGatewayService->charge();
$circuitBreaker->reportSuccess('payment.gateway');
} catch (Exception $e) {
$circuitBreaker->reportFailure('payment.gateway');
}
} else {
// Do something else
}