<?php
/** 
 * @package     VikRentItems
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2022 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://e4j.com
 */

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

JPluginHelper::importPlugin('vikrentitems');

/**
 * Factory application class.
 *
 * @since 1.2
 */
final class VRIFactory
{
	/**
	 * Application configuration handler.
	 *
	 * @var VRIConfigRegistry
	 */
	private static $config;

	/**
	 * Application platform handler.
	 *
	 * @var VRIPlatformInterface
	 */
	private static $platform;

	/**
	 * Cron jobs factory instance.
	 * 
	 * @var VRICronFactory
	 */
	private static $cronFactory;

	/**
	 * Class constructor.
	 * @private This object cannot be instantiated. 
	 */
	private function __construct()
	{
		// never called
	}

	/**
	 * Class cloner.
	 * @private This object cannot be cloned.
	 */
	private function __clone()
	{
		// never called
	}

	/**
	 * Returns the current configuration object.
	 *
	 * @return 	VRIConfigRegistry
	 */
	public static function getConfig()
	{
		// check if config class is already instantiated
		if (is_null(static::$config))
		{
			// cache instantiation
			static::$config = new VRIConfigRegistryDatabase([
				'db' => JFactory::getDbo(),
			]);
		}

		return static::$config;
	}

	/**
	 * Returns the current platform handler.
	 *
	 * @return 	VRIPlatformInterface
	 */
	public static function getPlatform()
	{
		// check if platform class is already instantiated
		if (is_null(static::$platform))
		{
			if (defined('ABSPATH') && function_exists('wp_die'))
			{
				// running WordPress platform
				static::$platform = new VRIPlatformOrgWordpress();
			}
			else
			{
				// running Joomla platform
				static::$platform = new VRIPlatformOrgJoomla();
			}
		}

		return static::$platform;
	}

	/**
	 * Returns the current cron factory.
	 *
	 * @return 	VRICronFactory
	 * 
	 * @since 	1.2
	 */
	public static function getCronFactory()
	{
		// check if cron factory class is already instantiated
		if (is_null(static::$cronFactory))
		{
			// create cron factory class and register the default folder
			static::$cronFactory = new VRICronFactory;
			static::$cronFactory->setIncludePaths(VRI_ADMIN_PATH . DIRECTORY_SEPARATOR . 'cronjobs');

			/**
			 * Trigger hook to allow third-party plugin to register custom folders in which
			 * VikRentItems should look for the creation of new cron job instances.
			 * 
			 * In example:
			 * $factory->addIncludePath($path);
			 * $factory->addIncludePaths([$path1, $path2, ...]);
			 * 
			 * @param   VRICronFactory  $factory  The cron jobs factory.
			 * 
			 * @return  void
			 * 
			 * @since 	1.2
			 */
			JFactory::getApplication()->triggerEvent('onCreateCronJobsFactoryVikRentItems', [static::$cronFactory]);
		}

		return static::$cronFactory;
	}
}
