<?php
namespace ModulesGarden\Servers\ZimbraEmail\App\Libs\Restrictions;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Restrictions\Interfaces\AbstractRestriction;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Restrictions\Interfaces\RuleInterface;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 07.11.19
 * Time: 09:58
 * Class Restriction
 */
class Restriction extends AbstractRestriction
{

    /**
     * {@inheritDoc}
     */
    public function check()
    {
        /**
         *
         * check if rule is valid
         */
        if($this->rule->isValid() !== false)
        {
            return true;
        }

        /**
         * set status as invalid
         */
        $this->setIsValid(self::STATUS_INVALID);

        /**
         *
         * set error message
         */
        $this->setErrorMessage($this->rule->getMessage());

        /**
         *
         * throw error if is enabled
         */
        if($this->isThrowErrorEnabled())
        {
            throw new \Exception($this->getErrorMessage());
        }

        /**
         *
         * return valid status
         */
        return $this->isValid();
    }
}