<?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\install\lib\core\model;
\defined('_JEXEC') or die;

class exceptions
{
	public function __construct() {}

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

	public static function set_handler(int $error_levels = E_ALL) // void
	{
		\set_error_handler([self::class, 'error_handler'], $error_levels);
	}

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

	public static function error_handler($code = '', $string = '', $file = '', $line = 0, $context = []) // void
	{
		if ($code or $string)
		{
			$error  = 'Error Code: '.$code.' ';
			$error .= 'Error: '.$string.' ';
			$error .= 'in file: '.$file.' ';
			$error .= 'on line '.$line;
			self::log($error);
		}
	}

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

	public static function log(string $string) // void
	{
		$path = JPATH_SITE.'/error_log';
		$date_string = \eorisis_setup\framework\install\lib\core\model\geotime::date_by_timezone()->format('Y/m/d h:i:s');
		$data = '['.$date_string.'] '.$string."\n";
		\file_put_contents($path, $data, FILE_APPEND);
	}

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

	public static function log_exception($exception) // void
	{
		$error  = 'Error Code: '.$exception->getCode().' ';
		$error .= 'Error: '.$exception->getMessage().' ';
		$error .= 'in file: '.$exception->getFile().' ';
		$error .= 'on line '.$exception->getLine();
		self::log($error);
	}
}