PHP code example of leeovery / laravel-idempotency

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

    

leeovery / laravel-idempotency example snippets


use LeeOvery\Idempotency\Facades\Idempotency;

Idempotency::once("order:{$order->id}:confirmed", fn () => $order->confirm());

use LeeOvery\Idempotency\Exceptions\AlreadyClaimed;

try {
    Idempotency::once($key, fn () => $workflow->start(), throw: true);
} catch (AlreadyClaimed) {
    // Started by an earlier delivery.
}

if (Idempotency::completed($key)) {
    return;
}

$receipt = $sms->send($message);

Idempotency::complete($key, ['receipt' => $receipt->id]);

try {
    Idempotency::claim($key, ['channel' => 'sms']);
} catch (AlreadyClaimed) {
    return;
}

use LeeOvery\Idempotency\Facades\Idempotency;

test('will confirm the order once', function (): void {
    $idempotency = Idempotency::fake();
    $order = Order::factory()->create();

    resolve(ConfirmOrderAction::class)($order);

    $idempotency->assertCompleted("order:{$order->id}:confirmed");
});
bash
php artisan vendor:publish --tag=idempotency-migrations
php artisan migrate