<?php

namespace DWS_QRWC_Deps\DeepWebSolutions\Framework\Utilities\Templating\Actions;

use DWS_QRWC_Deps\DeepWebSolutions\Framework\Foundations\Actions\Initializable\InitializableExtensionTrait;
use DWS_QRWC_Deps\DeepWebSolutions\Framework\Foundations\Actions\Initializable\InitializationFailureException;
use DWS_QRWC_Deps\DeepWebSolutions\Framework\Foundations\DependencyInjection\ContainerAwareInterface;
use DWS_QRWC_Deps\DeepWebSolutions\Framework\Foundations\Hierarchy\ChildInterface;
use DWS_QRWC_Deps\DeepWebSolutions\Framework\Foundations\PluginAwareInterface;
use DWS_QRWC_Deps\DeepWebSolutions\Framework\Utilities\Templating\TemplatingService;
use DWS_QRWC_Deps\DeepWebSolutions\Framework\Utilities\Templating\TemplatingServiceAwareInterface;
use DWS_QRWC_Deps\DeepWebSolutions\Framework\Utilities\Templating\TemplatingServiceAwareTrait;
use DWS_QRWC_Deps\Psr\Container\ContainerExceptionInterface;
use DWS_QRWC_Deps\Psr\Container\NotFoundExceptionInterface;
\defined( 'ABSPATH' ) || exit;
/**
 * Trait for setting the templating service on the using instance.
 *
 * @since   1.0.0
 * @version 1.0.0
 * @author  Antonius Hegyes <a.hegyes@deep-web-solutions.com>
 * @package DeepWebSolutions\WP-Framework\Utilities\Templating\Actions
 */
trait InitializeTemplatingServiceTrait {

	// region TRAITS
	use TemplatingServiceAwareTrait;
	use InitializableExtensionTrait;
	// endregion
	// region METHODS
	/**
	 * Try to automagically set a templating service on the instance.
	 *
	 * @since   1.0.0
	 * @version 1.0.0
	 *
	 * @throws  NotFoundExceptionInterface      Thrown if the container can't find an entry.
	 * @throws  ContainerExceptionInterface     Thrown if the container encounters some other error.
	 *
	 * @return  InitializationFailureException|null
	 */
	public function initialize_templating_service() : ?InitializationFailureException {
		if ( $this instanceof ChildInterface && $this->get_parent() instanceof TemplatingServiceAwareInterface ) {
			/* @noinspection PhpUndefinedMethodInspection */
			$service = $this->get_parent()->get_templating_service();
		} elseif ( $this instanceof ContainerAwareInterface ) {
			$service = $this->get_container()->get( TemplatingService::class );
		} elseif ( $this instanceof PluginAwareInterface && $this->get_plugin() instanceof ContainerAwareInterface ) {
			/* @noinspection PhpUndefinedMethodInspection */
			$service = $this->get_plugin()->get_container()->get( TemplatingService::class );
		} else {
			return new InitializationFailureException( 'Templating service initialization scenario not supported' );
		}
		$this->set_templating_service( $service );
		return null;
	}
	// endregion
}
