<?php

declare(strict_types=1);

namespace apexl\Io\modules\user\services;

final readonly class TokenExpiryCalculator
{
    public function fromNow(string $encodedToken): int
    {
        $sessionInfo = $this->getSessionInfoFromToken($encodedToken);

        return max($sessionInfo->exp - time(), 0);
    }

    private function getSessionInfoFromToken(string $encodedToken): object
    {
        [, $encodedSessionInfo] = explode('.', $encodedToken);

        return json_decode(base64_decode($encodedSessionInfo));
    }

}