<?php

namespace Opentalent\OtStats\ViewHelpers;


use Closure;
use Opentalent\OtCore\Website\OtPageRepository;
use Opentalent\OtStats\Settings\StatsSettingsRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

/**
 * Returns the current site matomo's id if any, null else
 *
 *     {namespace st=Opentalent\OtStats\ViewHelpers}
 *
 *     {st:matomoSiteId(slug: 'foo')}
 *
 * @package Opentalent\OtTemplating\ViewHelpers
 */
class MatomoSiteIdViewHelper extends AbstractViewHelper
{

    /**
     * -- This method is expected by Fluid --
     * Renders the content as html
     *
     * @param array $arguments
     * @param Closure $renderChildrenClosure
     * @param RenderingContextInterface $renderingContext
     * @return int|null
     */
    public static function renderStatic(
        array $arguments,
        Closure $renderChildrenClosure,
        RenderingContextInterface $renderingContext
    ) {
        $otPageRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(OtPageRepository::class);
        $rootPageUid = $otPageRepository->getCurrentSiteRootPageUid();

        $statsSettingsRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(StatsSettingsRepository::class);
        return $statsSettingsRepository->getMatomoSiteId($rootPageUid);
    }
}
