Newer
Older
gnexus-auth-client-php / src / Support / InMemoryStateStore.php
@Eugene Sukhodolskiy Eugene Sukhodolskiy 12 hours ago 1 KB Initial auth client package scaffold
<?php

declare(strict_types=1);

namespace GNexus\GAuth\Support;

use GNexus\GAuth\Contract\StateStoreInterface;

final class InMemoryStateStore implements StateStoreInterface
{
    /**
     * @var array<string, array{expires_at:\DateTimeImmutable, context:array<string, mixed>}>
     */
    private array $items = [];

    public function put(string $state, \DateTimeImmutable $expiresAt, array $context = []): void
    {
        $this->items[$state] = [
            'expires_at' => $expiresAt,
            'context' => $context,
        ];
    }

    public function has(string $state): bool
    {
        if (! isset($this->items[$state])) {
            return false;
        }

        if ($this->items[$state]['expires_at'] < new \DateTimeImmutable()) {
            unset($this->items[$state]);

            return false;
        }

        return true;
    }

    public function getContext(string $state): array
    {
        if (! $this->has($state)) {
            return [];
        }

        return $this->items[$state]['context'];
    }

    public function forget(string $state): void
    {
        unset($this->items[$state]);
    }
}