Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
67 / 67
GenericGeneratorConfig
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
11 / 11
29
100.00% covered (success)
100.00%
67 / 67
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
 validate
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
12 / 12
 validateGenerator
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
9 / 9
 validatePluginList
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 validatePlugin
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
9 / 9
 validateValdidatorList
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 validateValidator
100.00% covered (success)
100.00%
1 / 1
9
100.00% covered (success)
100.00%
22 / 22
 getName
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getClassName
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getPluginList
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getValidatorList
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
<?php
namespace Siesta\Config;
use Siesta\Model\ValidationLogger;
use Siesta\Util\ArrayUtil;
use Siesta\Validator\Validator;
/**
 * @author Gregor Müller
 */
class GenericGeneratorConfig
{
    const GENERATOR_INTERFACE = 'Siesta\Contract\Generator';
    const PLUGIN_INTERFACE = 'Siesta\Contract\Plugin';
    const ERROR_GENERATOR_HAS_NO_NAME = "Found generator without name.";
    const ERROR_GENERATOR_HAS_NO_NAME_CODE = 100;
    const ERROR_GENERATOR_CLASS_DOES_NOT_EXIST = "Generator class '%s' does not exist or is not (auto)loaded";
    const ERROR_GENERATOR_CLASS_DOES_NOT_EXIST_CODE = 101;
    const ERROR_GENERATOR_CLASS_DOES_NOT_IMPLEMENT = "Generator class '%s' does not implement Siesta\\Contract\\Generator";
    const ERROR_GENERATOR_CLASS_DOES_NOT_IMPLEMENT_CODE = 101;
    const ERROR_GENERATOR_PLUGIN_IS_NOT_ARRAY = "pluginList defined for '%s' is not an array";
    const ERROR_GENERATOR_PLUGIN_IS_NOT_ARRAY_CODE = 103;
    const ERROR_GENERATOR_PLUGIN_DOES_NOT_EXIST = "Plugin '%s' defined in '%s' does not exist or is not (auto) loaded";
    const ERROR_GENERATOR_PLUGIN_DOES_NOT_EXIST_CODE = 104;
    const ERROR_GENERATOR_PLUGIN_DOES_NOT_IMPLEMENT = "Plugin '%s' defined in '%s' does not implement Siesta\\Contract\\Plugin";
    const ERROR_GENERATOR_PLUGIN_DOES_NOT_IMPLEMENT_CODE = 105;
    const ERROR_GENERATOR_VALIDATOR_IS_NOT_ARRAY = "validatorList defined for '%s' is not an array";
    const ERROR_GENERATOR_VALIDATOR_IS_NOT_ARRAY_CODE = 106;
    const ERROR_GENERATOR_VALIDATOR_DOES_NOT_EXIST = "Validator '%s' defined in '%s' does not exist or is not (auto) loaded";
    const ERROR_GENERATOR_VALIDATOR_DOES_NOT_EXIST_CODE = 107;
    const ERROR_GENERATOR_VALIDATOR_DOES_NOT_IMPLEMENT = "Validator '%s' defined in '%s' does implement any Validator interface";
    const ERROR_GENERATOR_VALIDATOR_DOES_NOT_IMPLEMENT_CODE = 108;
    const NAME = "name";
    const CLASSNAME = "className";
    const PLUGIN_LIST = "pluginList";
    const VALIDATOR_LIST = "validatorList";
    /**
     * @var string
     */
    protected $name;
    /**
     * @var string
     */
    protected $className;
    /**
     * @var string[]
     */
    protected $pluginList;
    /**
     * @var string[]
     */
    protected $validatorList;
    /**
     * GeneratorConfig constructor.
     *
     * @param array $valueList
     */
    public function __construct(array $valueList)
    {
        $this->name = ArrayUtil::getFromArray($valueList, self::NAME);
        $this->className = ArrayUtil::getFromArray($valueList, self::CLASSNAME);
        $this->pluginList = ArrayUtil::getFromArray($valueList, self::PLUGIN_LIST);
        $this->validatorList = ArrayUtil::getFromArray($valueList, self::VALIDATOR_LIST);
    }
    /**
     * @param ValidationLogger $logger
     */
    public function validate(ValidationLogger $logger)
    {
        if (empty($this->name)) {
            $logger->error(self::ERROR_GENERATOR_HAS_NO_NAME, self::ERROR_GENERATOR_HAS_NO_NAME_CODE);
        }
        $this->validateGenerator($logger);
        if (!is_array($this->pluginList)) {
            $error = sprintf(self::ERROR_GENERATOR_PLUGIN_IS_NOT_ARRAY, $this->name);
            $logger->error($error, self::ERROR_GENERATOR_PLUGIN_IS_NOT_ARRAY_CODE);
        } else {
            $this->validatePluginList($logger);
        }
        if (!is_array($this->validatorList)) {
            $error = sprintf(self::ERROR_GENERATOR_VALIDATOR_IS_NOT_ARRAY, $this->name);
            $logger->error($error, self::ERROR_GENERATOR_VALIDATOR_IS_NOT_ARRAY_CODE);
        } else {
            $this->validateValdidatorList($logger);
        }
    }
    /**
     * @param ValidationLogger $logger
     */
    protected function validateGenerator(ValidationLogger $logger)
    {
        if (empty($this->className) || !class_exists($this->className)) {
            $error = sprintf(self::ERROR_GENERATOR_CLASS_DOES_NOT_EXIST, $this->className);
            $logger->error($error, self::ERROR_GENERATOR_CLASS_DOES_NOT_EXIST_CODE);
            return;
        }
        $reflect = new \ReflectionClass($this->className);
        if (!$reflect->implementsInterface(self::GENERATOR_INTERFACE)) {
            $error = sprintf(self::ERROR_GENERATOR_CLASS_DOES_NOT_IMPLEMENT, $this->className);
            $logger->error($error, self::ERROR_GENERATOR_CLASS_DOES_NOT_IMPLEMENT_CODE);
        }
    }
    /**
     * @param ValidationLogger $logger
     */
    protected function validatePluginList(ValidationLogger $logger)
    {
        foreach ($this->pluginList as $plugin) {
            $this->validatePlugin($logger, $plugin);
        }
    }
    /**
     * @param ValidationLogger $logger
     * @param string $plugin
     */
    protected function validatePlugin(ValidationLogger $logger, string $plugin)
    {
        if (!class_exists($plugin)) {
            $error = sprintf(self::ERROR_GENERATOR_PLUGIN_DOES_NOT_EXIST, $plugin, $this->getName());
            $logger->error($error, self::ERROR_GENERATOR_PLUGIN_DOES_NOT_EXIST_CODE);
            return;
        }
        $reflect = new \ReflectionClass($plugin);
        if (!$reflect->implementsInterface(self::PLUGIN_INTERFACE)) {
            $error = sprintf(self::ERROR_GENERATOR_PLUGIN_DOES_NOT_IMPLEMENT, $plugin, $this->getName());
            $logger->error($error, self::ERROR_GENERATOR_PLUGIN_DOES_NOT_IMPLEMENT_CODE);
        }
    }
    /**
     * @param ValidationLogger $logger
     */
    protected function validateValdidatorList(ValidationLogger $logger)
    {
        foreach ($this->validatorList as $validator) {
            $this->validateValidator($logger, $validator);
        }
    }
    public function validateValidator(ValidationLogger $logger, $validator)
    {
        if (!class_exists($validator)) {
            $error = sprintf(self::ERROR_GENERATOR_VALIDATOR_DOES_NOT_EXIST, $validator, $this->getName());
            $logger->error($error, self::ERROR_GENERATOR_VALIDATOR_DOES_NOT_EXIST_CODE);
            return;
        }
        $reflect = new \ReflectionClass($validator);
        if ($reflect->implementsInterface(Validator::DATAMODEL_VALIDATOR_INTERFACE)) {
            return;
        }
        if ($reflect->implementsInterface(Validator::ENTIY_VALIDATOR_INTERFACE)) {
            return;
        }
        if ($reflect->implementsInterface(Validator::ATTRIBUTE_VALIDATOR_INTERFACE)) {
            return;
        }
        if ($reflect->implementsInterface(Validator::REFERENCE_VALIDATOR_INTERFACE)) {
            return;
        }
        if ($reflect->implementsInterface(Validator::INDEX_VALIDATOR_INTERFACE)) {
            return;
        }
        if ($reflect->implementsInterface(Validator::COLLECTION_VALIDATOR_INTERFACE)) {
            return;
        }
        if ($reflect->implementsInterface(Validator::COLLECTION_MANY_VALIDATOR_INTERFACE)) {
            return;
        }
        $error = sprintf(self::ERROR_GENERATOR_VALIDATOR_DOES_NOT_IMPLEMENT, $validator, $this->getName());
        $logger->error($error, self::ERROR_GENERATOR_VALIDATOR_DOES_NOT_IMPLEMENT_CODE);
    }
    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
    /**
     * @return string
     */
    public function getClassName()
    {
        return $this->className;
    }
    /**
     * @return string[]
     */
    public function getPluginList()
    {
        return $this->pluginList;
    }
    /**
     * @return string[]
     */
    public function getValidatorList()
    {
        return $this->validatorList;
    }
}