PHP code example of azaharizaman / nexus-idempotency

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

    

azaharizaman / nexus-idempotency example snippets


$service = new IdempotencyService($store, $store, $clock, IdempotencyPolicy::default());

$decision = $service->begin($tenantId, $operationRef, $clientKey, $fingerprint);

if ($decision->outcome === BeginOutcome::Replay) {
    return $decision->replayResult; // replay cached outcome
}

if ($decision->outcome === BeginOutcome::InProgress) {
    // Another in-flight execution for the same key + fingerprint; surface 409 + Retry-After in Layer 3.
    return;
}

// BeginOutcome::FirstExecution — run domain command, then complete using the attempt bound to this reservation:
$service->complete(
    $tenantId,
    $operationRef,
    $clientKey,
    $fingerprint,
    $decision->record->attemptToken,
    new ResultEnvelope($json),
);