PHP code example of rasuvaeff / yii3-idempotency

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

    

rasuvaeff / yii3-idempotency example snippets


use Rasuvaeff\Yii3Idempotency\HeaderIdempotencyKeyExtractor;
use Rasuvaeff\Yii3Idempotency\IdempotencyMiddleware;
use Rasuvaeff\Yii3Idempotency\InMemoryIdempotencyStorage;
use Rasuvaeff\Yii3Idempotency\RequestAttributeScopeResolver;

$middleware = new IdempotencyMiddleware(
    keyExtractor: new HeaderIdempotencyKeyExtractor(),
    storage: new InMemoryIdempotencyStorage($clock),
    responseFactory: $responseFactory,
    clock: $clock,
    // 

use Rasuvaeff\Yii3Idempotency\DefaultFailureClassifier;
use Rasuvaeff\Yii3Idempotency\FailureKind;
use Rasuvaeff\Yii3Idempotency\IdempotencyMiddleware;

$middleware = new IdempotencyMiddleware(
    keyExtractor: new HeaderIdempotencyKeyExtractor(),
    storage: $storage,
    responseFactory: $responseFactory,
    clock: $clock,
    scopeResolver: new RequestAttributeScopeResolver(attribute: 'user'),
    domainFailureRenderer: $renderer,                     // your DomainFailureRenderer
    failureClassifier: new DefaultFailureClassifier([     // optional overrides
        MisconfiguredGatewayException::class => FailureKind::Bug,
    ]),
);

use Rasuvaeff\Yii3Idempotency\RequestAttributeScopeResolver;

// the authenticated principal your auth middleware put in the request attribute
scopeResolver: new RequestAttributeScopeResolver(attribute: 'user');

// the attribute holds a user object rather than an identifier
scopeResolver: new RequestAttributeScopeResolver(
    attribute: 'user',
    identity: static fn (mixed $user): ?int => $user?->getId(),
);

use Rasuvaeff\Yii3Idempotency\SharedKeyspaceScopeResolver;

scopeResolver: new SharedKeyspaceScopeResolver();

use Rasuvaeff\Yii3Idempotency\CompositeScopeResolver;
use Rasuvaeff\Yii3Idempotency\IdempotencyScope;
use Rasuvaeff\Yii3Idempotency\RequestTargetScopeResolver;

// caller + "METHOD /path" — the wiring the bundled config builds by default
scopeResolver: new CompositeScopeResolver(
    new RequestAttributeScopeResolver(attribute: 'user'),
    new RequestTargetScopeResolver(),
);

// caller + an explicit namespace shared by related endpoints
scopeResolver: new CompositeScopeResolver(
    new RequestAttributeScopeResolver(attribute: 'user'),
    new IdempotencyScope('orders'),
);

use Rasuvaeff\Yii3Idempotency\PayloadIdempotencyKeyExtractor;

$extractor = new PayloadIdempotencyKeyExtractor('command.orderId');

// config/params.php
return [
    'rasuvaeff/yii3-idempotency' => [
        'headerName' => 'Idempotency-Key',
        'policy' => 'pass_through', // or 'reject'
        'ttlSeconds' => 3600,
        'methods' => ['POST', 'PUT', 'PATCH'], // methods idempotency applies to
        // REQUIRED — the container refuses to build the middleware while this is null.
        // A request-attribute name: keys are namespaced by the principal found there.
        // false: every caller shares one keyspace (pre-2.0 behaviour) — see "Caller scoping".
        'callerAttribute' => 'user',
        'anonymousCaller' => 'anonymous', // namespace for requests carrying no principal
        'scope' => 'auto', // 'auto' — per "METHOD /path"; null — one namespace for every endpoint; any other string — explicit scope name
    ],
];