<?php

declare(strict_types=1);

namespace Enot;

// Avoid direct access to this piece of code
defined('ABSPATH') || exit;

use Throwable;

final class EnotCache
{
	/** @var array<string, string> */
	private static array $throwable_cache = [];

	private static function hash(string $string): string
	{
		return md5($string);
	}

	private static function serialize(Throwable $e): string
	{
		return serialize([
			$e->getMessage(),
			$e->getCode(),
			$e->getFile(),
			$e->getLine(),
			$e->getTraceAsString(),
		]);
	}

	private static function key(Throwable $e): string
	{
		return self::hash(self::serialize($e));
	}

	public static function add(Throwable $e): void
	{
		self::$throwable_cache[self::key($e)] = $e;
	}

	public static function exists(Throwable $e): bool
	{
		return array_key_exists(self::key($e), self::$throwable_cache);
	}
}
