<?php

namespace Opentalent\OtTemplating\ViewHelpers\Request;

use Closure;
use Opentalent\OtCore\Service\OpentalentEnvService;
use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;

/**
 *   Returns an opentalent environment variable from the OpentalentEnvService
 *
 *     {namespace ot=Opentalent\OtTemplating\ViewHelpers}
 *
 *     {ot:request.getOtEnvVar(argument: 'name')}
 *
 * @package Opentalent\OtTemplating\ViewHelpers
 */
class GetOtEnvVarViewHelper extends OtAbstractViewHelper {

    private OpentalentEnvService $opentalentEnvService;

    /**
     * @param OpentalentEnvService $opentalentEnvService
     */
    public function injectOpentalentEnvService(OpentalentEnvService $opentalentEnvService)
    {
        $this->opentalentEnvService = $opentalentEnvService;
    }

    /**
     * -- This method is expected by Fluid --
     * Declares the viewhelper's parameters
     */
    public function initializeArguments()
    {
        $this->registerArgument('argument',
            'string',
            "The argument's name",
            true);
    }

    /**
     * -- This method is expected by Fluid --
     * Renders the content as html
     *
     * @param array $arguments
     * @param Closure $renderChildrenClosure
     * @param RenderingContextInterface $renderingContext
     * @return string
     */
    public function render() {
        $argument = $this->arguments['argument'];
        return $this->opentalentEnvService->get($argument);
    }
}
