Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
34 / 34
GenericConfigLoader
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
6 / 6
14
100.00% covered (success)
100.00%
34 / 34
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 setConfigFile
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getConfigFile
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 loadAndValidate
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
8 / 8
 getGenericGeneratorConfigList
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
14 / 14
 initializeGenericGeneratorConfig
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
<?php
namespace Siesta\Config;
use Siesta\Model\ValidationLogger;
use Siesta\Util\ArrayUtil;
use Siesta\Util\File;
/**
 * @author Gregor Müller
 */
class GenericConfigLoader
{
    const ERROR_CONFIG_DOES_NOT_EXIST = "Generator Config file '%s' does not exist.";
    const ERROR_CONFIG_DOES_NOT_EXIST_CODE = 1;
    const ERROR_CONFIG_FILE_IS_EMPTY = "Generator Config file '%s' is invalid or empty";
    const ERROR_CONFIG_FILE_IS_EMPTY_CODE = 2;
    const ERROR_CONFIG_FILE_MISSES_GEN = "Generator Config file '%s' has no 'generatorList' array";
    const ERROR_CONFIG_FILE_MISSES_GEN_CODE = 3;
    const ERROR_NO_GENERIC_GENERATORS_DEFINED = "Generator Config file '%s' has no generators defined.";
    const ERROR_NO_GENERIC_GENERATORS_DEFINED_CODE = 4;
    const GENERATOR_LIST = "generatorList";
    /**
     * @var null|File
     */
    protected $configFile;
    public function __construct()
    {
    }
    /**
     * @param File $file
     */
    public function setConfigFile(File $file)
    {
        $this->configFile = $file;
    }
    /**
     * @return File
     */
    protected function getConfigFile() : File
    {
        if ($this->configFile === null) {
            $this->configFile = new File(__DIR__ . "/siesta.generator.config.json");
        }
        return $this->configFile;
    }
    /**
     * @param ValidationLogger $logger
     *
     * @return GenericGeneratorConfig[]
     */
    public function loadAndValidate(ValidationLogger $logger)
    {
        $genericGeneratorConfigList = $this->getGenericGeneratorConfigList($logger, $this->getConfigFile());
        if ($genericGeneratorConfigList === null) {
            return [];
        }
        $configList = $this->initializeGenericGeneratorConfig($logger, $genericGeneratorConfigList);
        if (sizeof($configList) === 0) {
            $error = sprintf(self::ERROR_NO_GENERIC_GENERATORS_DEFINED, $this->configFile->getAbsoluteFileName());
            $logger->error($error, self::ERROR_NO_GENERIC_GENERATORS_DEFINED_CODE);
        }
        return $configList;
    }
    /**
     * @param ValidationLogger $logger
     * @param File $file
     *
     * @return null|string[]
     */
    protected function getGenericGeneratorConfigList(ValidationLogger $logger, File $file)
    {
        if (!$file->exists()) {
            $error = sprintf(self::ERROR_CONFIG_DOES_NOT_EXIST, $file->getAbsoluteFileName());
            $logger->error($error, self::ERROR_CONFIG_DOES_NOT_EXIST_CODE);
            return null;
        }
        $configValues = $file->loadAsJSONArray();
        if ($configValues === null) {
            $error = sprintf(self::ERROR_CONFIG_FILE_IS_EMPTY, $file->getAbsoluteFileName());
            $logger->error($error, self::ERROR_CONFIG_FILE_IS_EMPTY_CODE);
            return null;
        }
        $genericGeneratorConfigList = ArrayUtil::getFromArray($configValues, self::GENERATOR_LIST);
        if ($genericGeneratorConfigList === null || !is_array($genericGeneratorConfigList)) {
            $error = sprintf(self::ERROR_CONFIG_FILE_MISSES_GEN, $file->getAbsoluteFileName());
            $logger->error($error, self::ERROR_CONFIG_FILE_MISSES_GEN_CODE);
        }
        return $genericGeneratorConfigList;
    }
    /**
     * @param ValidationLogger $logger
     * @param array $genericGeneratorConfigList
     *
     * @return GenericGeneratorConfig[]
     */
    protected function initializeGenericGeneratorConfig(ValidationLogger $logger, array $genericGeneratorConfigList) : array
    {
        $genericGeneratorConfig = [];
        foreach ($genericGeneratorConfigList as $genericGeneratorConfigValues) {
            $genericConfig = new GenericGeneratorConfig($genericGeneratorConfigValues);
            $genericConfig->validate($logger);
            $genericGeneratorConfig[] = $genericConfig;
        }
        return $genericGeneratorConfig;
    }
}