Session.php 1.0 KB
<?php

namespace Lackoxygen\TiktokShop\Supervisor\Session;

use Carbon\Carbon;
use Lackoxygen\TiktokShop\Contracts\SessionInterface;

class Session implements SessionInterface
{
    use Swap;

    protected Store $store;

    protected string $index = '';

    protected string $appKey = '';

    public function __construct(string $appKey, Store $store)
    {
        $this->appKey = $appKey;

        $this->store = $store;

        $this->index = $this->id();
    }

    public function id(): string
    {
        return md5(serialize($this->store));
    }

    public function unEffective(): bool
    {
        return !$this->effective();
    }

    public function effective(): bool
    {
        return $this->store->getExpiredAt()->gt(Carbon::now());
    }

    public function discard()
    {
        $this->store->setExpiredAt(Carbon::now()->subMinutes(30));
    }

    public function watch(): bool
    {
        return $this->index !== $this->id();
    }

    public function appKey(): string
    {
        return $this->appKey;
    }
}