Viewing File: /usr/local/cpanel/base/frontend/jupiter/ls_web_cache_manager/core/View/Model/SettingsViewModel.php

<?php

/** ******************************************
 * LiteSpeed Web Cache Management Plugin for cPanel
 *
 * @author    Michael Alegre
 * @copyright 2018-2025 LiteSpeed Technologies, Inc.
 * ******************************************* */

namespace LsUserPanel\View\Model;

use LsUserPanel\Lsc\Context\UserContext;
use LsUserPanel\Lsc\UserLogger;
use LsUserPanel\Lsc\UserLSCMException;

class SettingsViewModel
{

    /**
     * @var string
     */
    const FLD_LOG_FILE = 'logFile';

    /**
     * @var string
     */
    const FLD_CURR_LOG_LVL = 'currLogLvl';

    /**
     * @var string
     */
    const FLD_LOG_LVLS = 'logLvls';

    /**
     * @since 2.4
     * @var   string
     */
    const FLD_REDIS_ONLY = 'redisOnly';

    /**
     * @var string
     */
    const FLD_ERR_MSGS = 'errMsgs';

    /**
     * @var string
     */
    const FLD_SUCC_MSGS = 'succMsgs';

    /**
     * @var array
     */
    private array $tplData = [];

    /**
     *
     * @param int $redisOnly
     *
     * @throws UserLSCMException  Thrown indirectly by $this->init() call.
     */
    public function __construct( int $redisOnly )
    {
        $this->init($redisOnly);
    }

    /**
     *
     * @param int $redisOnly
     *
     * @throws UserLSCMException  Thrown indirectly by $this->setLogData() call.
     * @throws UserLSCMException  Thrown indirectly by $this->setMsgData() call.
     */
    private function init( $redisOnly ): void
    {
        $this->tplData[self::FLD_REDIS_ONLY] = $redisOnly;

        $this->setLogData();
        $this->setMsgData();
    }

    /**
     *
     * @param string $field
     *
     * @return null|mixed
     */
    public function getTplData( string $field )
    {
        return $this->tplData[$field] ?? null;
    }

    /**
     *
     * @throws UserLSCMException  Thrown indirectly by UserContext::getOption()
     *     call.
     */
    private function setLogData(): void
    {
        $options = UserContext::getOption();

        $this->tplData[self::FLD_LOG_FILE]     = $options->getLogFile();
        $this->tplData[self::FLD_CURR_LOG_LVL] = $options->getLogFileLvl();
        $this->tplData[self::FLD_LOG_LVLS]     = [
            'None'    => UserLogger::L_NONE,
            'Error'   => UserLogger::L_ERROR,
            'Warning' => UserLogger::L_WARN,
            'Notice'  => UserLogger::L_NOTICE,
            'Info'    => UserLogger::L_INFO,
            'Verbose' => UserLogger::L_VERBOSE,
            'Debug'   => UserLogger::L_DEBUG
        ];
    }

    /**
     *
     * @throws UserLSCMException  Thrown indirectly by UserLogger::getUiMsgs()
     *     call.
     * @throws UserLSCMException  Thrown indirectly by UserLogger::getUiMsgs()
     *     call.
     */
    private function setMsgData(): void
    {
        $this->tplData[self::FLD_ERR_MSGS]  =
            UserLogger::getUiMsgs(UserLogger::UI_ERR);
        $this->tplData[self::FLD_SUCC_MSGS] =
            UserLogger::getUiMsgs(UserLogger::UI_SUCC);
    }

    /**
     *
     * @return string
     */
    public function getTpl(): string
    {
        return realpath(__DIR__ . '/../Tpl') . '/Settings.tpl';
    }

}
Back to Directory File Manager