<?php
/**
 * 2014 - 2023 Watt Is It
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the MIT License X11
 * that is bundled with this package in the file LICENSE.md.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/mit-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to contact@paygreen.fr so we can send you a copy immediately.
 *
 * @author    PayGreen <contact@paygreen.fr>
 * @copyright 2014 - 2023 Watt Is It
 * @license   https://opensource.org/licenses/mit-license.php MIT License X11
 * @version   1.3.7
 *
 */

namespace PGI\Impact\PGFramework\Services\Handlers;

use PGI\Impact\PGLog\Interfaces\LoggerInterface;
use PGI\Impact\PGSystem\Services\Container;
use PGI\Impact\PGFramework\Components\Aggregator as AggregatorComponent;
use Exception;

/**
 * Class HookHandler
 * @package PGFramework\Services\Handlers
 */
class HookHandler
{
    /** @var AggregatorComponent */
    private $hookAggregator;

    /** @var LoggerInterface */
    private $logger;

    private $hooks = array();

    public function __construct(AggregatorComponent $hookAggregator, LoggerInterface $logger)
    {
        $this->hookAggregator = $hookAggregator;
        $this->logger = $logger;
    }

    /**
     * @param string $hookName
     * @param string $methodName
     * @return callable
     */
    public function addHookName($hookName, $methodName)
    {
        $hookIdentifier = md5("$hookName-$methodName");

        $this->hooks[$hookIdentifier] = array(
            'hook' => $hookName,
            'method' => $methodName
        );

        return array($this, $hookIdentifier);
    }

    /**
     * @param string $hookIdentifier
     * @param array $arguments
     * @return mixed|void
     * @throws Exception
     */
    public function __call($hookIdentifier, array $arguments = array())
    {
        try {
            if (!isset($this->hooks[$hookIdentifier])) {
                throw new Exception("Unresolved hook or method not found : '$hookIdentifier'.");
            }

            $hookName = $this->hooks[$hookIdentifier]['hook'];
            $methodName = $this->hooks[$hookIdentifier]['method'];

            return $this->run($hookName, $methodName, $arguments);
        } catch (Exception $exception) {
            catchLowLevelImpactException($exception, false);
        }
    }

    /**
     * @param string $hookName
     * @param string $methodName
     * @param array $arguments
     * @return mixed
     * @throws Exception
     */
    public function run($hookName, $methodName, array $arguments = array())
    {
        $this->logger->debug("Running method '$methodName' on hook '$hookName'.");

        $service = $this->hookAggregator->getService($hookName);

        return call_user_func_array(array($service, $methodName), $arguments);
    }
}
