<?php
/**
 * Views factory for creating view instances.
 *
 * @package Awesomemotive\Rewardswp\Factories
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Factories;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Awesomemotive\Rewardswp\Abstracts\AbstractView;
use Awesomemotive\Rewardswp\Interfaces\ViewInterface;
use DI\Container;
use Exception;

class ViewsFactory {
	/**
	 * Dependency injection container.
	 *
	 * @since 1.0.0
	 *
	 * @var Container
	 */
	private Container $container;

	public function __construct( Container $container ) {
		$this->container = $container;
	}

	/**
	 * Retrieves an instance of a View object based on the class name.
	 *
	 * @since 1.0.0
	 *
	 * @param string|ViewInterface $class_name The view class name.
	 *
	 * @return ViewInterface|AbstractView|null The View object instance, or null if not found.
	 */
	function get_view( $class_name ): ?ViewInterface {
		// Check if it is the class name, if so, return right away.
		try {
			return $this->container->get( $class_name );
		} catch ( Exception $exception ) {
			return null;
		}
	}
}
