<?php
namespace Core;

class Token
{
	public static function generate()
	{
		if(!Session::exists('tokens'))
		{
			Session::put('tokens', []);
		}

		$tokens = Session::get('tokens');

		if(count($tokens) >= 10)
		{
			array_shift($tokens);
		}

		$tokens[] = md5(uniqid());

		Session::put('tokens', $tokens);

		return end($tokens);
	}

	public static function check($token)
	{
		$tokenName = 'tokens';
		$tokenNow = $token;

		if(Session::exists($tokenName))
		{
			if(in_array($tokenNow, Session::get($tokenName)))
			{
				Session::delete($tokenName);
				return true;
			}
		}

		return false;
	}
}
