<?php
/*
 * Software Name: Eorisis Setup
 * Software URI: https://eorisis.com/shop/software/joomla/setup
 * Author: Eorisis
 * Author URI: https://eorisis.com
 * Copyright: Copyright (C) 2011-2025 EORISIS. All Rights Reserved.
 * License: GNU General Public License version 2; See https://eorisis.com/gpl
 * Terms of Service: https://eorisis.com/tos
 */
namespace eorisis_setup\framework\lib\core\model;
\defined('_JEXEC') or die;

use json_decode;
use eorisis_setup\framework\lib\core\cms\application;
use eorisis_setup\framework\lib\core\model\database;
use eorisis_setup\framework\lib\core\model\params;
use eorisis_setup\framework\lib\core\model\clean;

class plugin
{
	public function __construct() {}

	// --------------------------------------------------

	public static function get_authcode(string $element, string $folder) : string
	{
		$dbo = application::dbo();
		$query = $dbo->getQuery(true);
		$query->select($dbo->quoteName('params'));
		$query->from($dbo->quoteName('#__extensions'));
		$query->where([
			$dbo->quoteName('element').'='.$dbo->quote($element),
			$dbo->quoteName('folder').'='.$dbo->quote($folder),
			$dbo->quoteName('type').'='.$dbo->quote('plugin')
		]);
		$dbo->setQuery($query);
		$result = database::get_first_result($dbo);
		if ($result['error'])
		{
			application::add_backend_message($result['error']);
			return '';
		}

		$item = (array)json_decode((string)$result['record'], true);
		return clean::auth_code(params::get('auth_token', $item));
	}
}
