<?php

declare(strict_types=1);

namespace GNexus\GAuth\Support;

use GNexus\GAuth\Contract\PkceStoreInterface;

final class InMemoryPkceStore implements PkceStoreInterface
{
    /**
     * @var array<string, array{verifier:string, expires_at:\DateTimeImmutable}>
     */
    private array $items = [];

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

    public function get(string $state): ?string
    {
        if (! isset($this->items[$state])) {
            return null;
        }

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

            return null;
        }

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

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

