Public API

Resolving a Cadence Engine

Using the Facade

use Kodefarmers\Cadence\Facades\Cadence;

$cadence = Cadence::driver();

Using Dependency Injection

use Kodefarmers\Cadence\CadenceManager;

class LoginService
{
    public function __construct(
        private readonly CadenceManager $cadence,
    ) {
    }

    public function handle(string $key): void
    {
        $cadence = $this->cadence->driver();

        // ...
    }
}

Recording Failures

$result = $cadence->recordFailure($key);

Returns a CadenceResult containing:

PropertyDescription
attemptCurrent attempt count.
violationCountCurrent violation count.
delayDelay applied in seconds.
isLockedWhether the key is currently locked.

Recording Success

$cadence->recordSuccess($key);

Resets the recorded attempts and removes any active lock.

Querying State

$cadence->ensureNotLocked($key);

$cadence->isLocked($key);

$cadence->remainingBackoff($key);

$cadence->attempts($key);

ensureNotLocked()

Throws CadenceLockedException if the key is currently locked.

isLocked()

Returns whether the key is currently locked.

remainingBackoff()

Returns the remaining lock duration in seconds.

attempts()

Returns the current recorded attempt count.

CadenceLockedException

When a key is locked, ensureNotLocked() throws CadenceLockedException.

The exception exposes:

MethodDescription
key()The locked key.
retryAfter()Seconds until the lock expires.
attempts()The attempt count when locked.
violationCount()The violation count when locked.