<?php

namespace Sibs\Stargate\Traits;

use Sibs\Stargate\Loggers\SibsLogger;

trait SuppressLogTrait
{
    /**
     * @var bool
     */
    protected $suppressLog = false;

    protected static $DEBUG = 'debug';
    protected static $INFO = 'info';
    protected static $WARNING = 'warning';
    protected static $ERROR = 'error';

    /**
     * Set the value of suppressLog.
     *
     * @return self
     */
    public function setSuppressLog(bool $suppressLog)
    {
        $this->suppressLog = $suppressLog;

        return $this;
    }

    /**
     * Validate and Log.
     */
    protected function validateAndLog(bool $validate, string $logMessage = ''): bool
    {
        $logType = (($validate) ? self::$DEBUG : self::$WARNING);
        $this->selfLog($logType, $logMessage.' = '.(($validate) ? 'true' : 'false'));

        return $validate;
    }

    /**
     * Self Log Log.
     */
    protected function selfLog(string $logType, string $logMessage = '')
    {
        if ($this->suppressLog || !isset($this->logger) || !is_object($this->logger) || !method_exists($this->logger, 'addRecord')) {
            return;
        }

        SibsLogger::addRecord($logType, $logMessage, 4);
    }
}
