<commit_before><?php declare(strict_types=1);

namespace App\Service;

use Doctrine\DBAL\Connection as Db;
use Predis\Client as Predis;

class UserCacheService
{
	const TTL = 2592000;

	protected Db $db;
	protected Predis $predis;
	protected bool $is_cli;

	protected array $local = [];

	public function __construct(
		Db $db,
		Predis $predis
	)
	{
		$this->db = $db;
		$this->predis = $predis;

		$this->is_cli = php_sapi_name() === 'cli' ? true : false;
	}

	public function clear(int $id, string $schema):void
	{
		if (!$id)
		{
			return;
		}

		$redis_key = $schema . '_user_' . $id;

		$this->predis->del($redis_key);
		unset($this->local[$schema][$id]);

		return;
	}

	public function is_active_user(int $id, string $schema):bool
	{
		$user = $this->get($id, $schema);
		return in_array($user['status'], [1, 2]);
	}

	public function get_role(int $id, string $schema):string
	{
		return $this->get($id, $schema)['role'];
	}

	/**
	 * TEMP FIX
	 */

	public function get(int $id, string $schema):array
	{
		$user = $this->temp_get($id, $schema);
		$user['code'] ??= $user['letscode'];
		$user['role'] ??= $user['accountrole'];

		if ($user['role'] === 'interlets')
		{
			$user['role'] = 'guest';
		}

		$user['periodic_overview_en'] ??= $user['cron_saldo'];
		$user['balance'] ??= $user['saldo'];

		return $user;
	}

	public function temp_get(int $id, string $schema):array
	{
		if (!$id)
		{
			return [];
		}

		$redis_key = $schema . '_user_' . $id;

		if (isset($this->local[$schema][$id]) && !$this->is_cli)
		{
			return $this->local[$schema][$id];
		}

		if ($this->predis->exists($redis_key))
		{
			return $this->local[$schema][$id] = unserialize($this->predis->get($redis_key));
		}

		$user = $this->read_from_db($id, $schema);

		if (isset($user))
		{
			$this->predis->set($redis_key, serialize($user));
			$this->predis->expire($redis_key, self::TTL);
			$this->local[$schema][$id] = $user;
		}

		return $user;
	}

	protected function read_from_db(int $id, string $schema):array<commit_msg>remove temp user cache fix<commit_after><?php declare(strict_types=1);

namespace App\Service;

use Doctrine\DBAL\Connection as Db;
use Predis\Client as Predis;

class UserCacheService
{
	const TTL = 2592000;

	protected Db $db;
	protected Predis $predis;
	protected bool $is_cli;

	protected array $local = [];

	public function __construct(
		Db $db,
		Predis $predis
	)
	{
		$this->db = $db;
		$this->predis = $predis;

		$this->is_cli = php_sapi_name() === 'cli' ? true : false;
	}

	public function clear(int $id, string $schema):void
	{
		if (!$id)
		{
			return;
		}

		$redis_key = $schema . '_user_' . $id;

		$this->predis->del($redis_key);
		unset($this->local[$schema][$id]);

		return;
	}

	public function is_active_user(int $id, string $schema):bool
	{
		$user = $this->get($id, $schema);
		return in_array($user['status'], [1, 2]);
	}

	public function get_role(int $id, string $schema):string
	{
		return $this->get($id, $schema)['role'];
	}

	public function get(int $id, string $schema):array
	{
		if (!$id)
		{
			return [];
		}

		$redis_key = $schema . '_user_' . $id;

		if (isset($this->local[$schema][$id]) && !$this->is_cli)
		{
			return $this->local[$schema][$id];
		}

		if ($this->predis->exists($redis_key))
		{
			return $this->local[$schema][$id] = unserialize($this->predis->get($redis_key));
		}

		$user = $this->read_from_db($id, $schema);

		if (isset($user))
		{
			$this->predis->set($redis_key, serialize($user));
			$this->predis->expire($redis_key, self::TTL);
			$this->local[$schema][$id] = $user;
		}

		return $user;
	}

	protected function read_from_db(int $id, string $schema):array
{'fraction_non_alphanumeric': 0.20126131066630107, 'fraction_numerical': 0.005483959418700302, 'mean_word_length': 4.038674033149171, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6866104', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1411, 'n_words': 279}