PHP code example of luucasfzs / laravel-circuit-breaker
1. Go to this page and download the library: Download luucasfzs/laravel-circuit-breaker 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/ */
luucasfzs / laravel-circuit-breaker example snippets
class BuyArticleOperation {
/** @var PaymentsGateway */
private $paymentsGateway;
/** @var DelayedPaymentsGateway */
private $delayedPaymentsGateway;
public function process(string $orderId)
{
// doing stuff with my order and then...
try {
$this->paymentsGateway->attempt($orderId);
} catch (PaymentsGatewayException $e) {
// something went wrong, let's switch the payment
// to the "delayed" queue system
$this->delayedPaymentsGateway->queue($orderId);
}
}
}
use CircuitBreaker;
use My\Namespace\PaymentsGateway;
use My\Namespace\DelayedPaymentsGateway;
class BuyArticleOperation {
/** @var PaymentsGateway */
private $paymentsGateway;
/** @var DelayedPaymentsGateway */
private $delayedPaymentsGateway;
public function process(string $orderId)
{
if(CircuitBreaker::isAvailable(PaymentsGateway::class)) {
try {
$this->paymentsGateway->attempt($orderId);
} catch (PaymentsGatewayException $e) {
// something went wrong, let's switch the payment
// to the "delayed" queue system and report that
// the default gateway is not working!
$this->delayedPaymentsGateway->queue($orderId);
CircuitBreaker::reportFailure(PaymentsGateway::class);
}
// there's nothing we can do here anymore
return;
}
// we already know that the service is disabled, so we
// can queue the payment process on the delayed queue
// directly, without letting our users wait more
$this->delayedPaymentsGateway->queue($orderId);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.