Commit b259eaec authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Amélioration des exceptions concernant la config

parent ea880f9a
Loading
Loading
Loading
Loading
+20 −1
Original line number Diff line number Diff line
@@ -6,9 +6,28 @@ use UnicaenApp\Exception\RuntimeException;

class ConfigException extends RuntimeException
{
    public static function missingKeyInImport($key, $name = null)
    {
        return $name ?
            new static("Le tableau de config de l'import '$name' doit contenir une clé '$key'") :
            new static("Le tableau de config d'un import ne contient pas la clé '$key' attendue");
    }

    public static function missingKeyInSynchro($key, $name = null)
    {
        return $name ?
            new static("Le tableau de config de la synchro '$name' doit contenir une clé '$key'") :
            new static("Le tableau de config d'une synchro ne contient pas la clé '$key' attendue");
    }

    public static function missingRootKey($key)
    {
        return new static("La config de l'appli doit contenir la clé '$key' contenant la config du module");
    }

    public static function missingKey($key)
    {
        return new static("Clé de config '$key' introuvable");
        return new static("Le tableau de config du module doit contenir la clé '$key'");
    }

    public static function missingKeyInConnections($key)
+17 −7
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ use Zend\ServiceManager\ServiceLocatorInterface;

class ConfigFactory
{
    const CONFIG_KEY = 'import';
    const CONFIG_ROOT_KEY = 'import';

    /**
     * @var ServiceLocatorInterface
@@ -74,8 +74,8 @@ class ConfigFactory
    {
        $appConfig = $this->container->get('Config');

        if (!array_key_exists($key = self::CONFIG_KEY, $appConfig)) {
            throw ConfigException::missingKey($key);
        if (!array_key_exists($key = self::CONFIG_ROOT_KEY, $appConfig)) {
            throw ConfigException::missingRootKey($key);
        }

        return $appConfig[$key];
@@ -185,11 +185,16 @@ class ConfigFactory
     */
    private function createImport(array $importConfig)
    {
        if (!array_key_exists($key = 'name', $importConfig)) {
            throw ConfigException::missingKeyInImport($key);
        }
        $name = $importConfig['name'];

        if (!array_key_exists($key = 'source', $importConfig)) {
            throw ConfigException::missingKey($key);
            throw ConfigException::missingKeyInImport($key, $name);
        }
        if (!array_key_exists($key = 'destination', $importConfig)) {
            throw ConfigException::missingKey($key);
            throw ConfigException::missingKeyInImport($key, $name);
        }

        $source = $this->createSource($importConfig['source']);
@@ -222,11 +227,16 @@ class ConfigFactory
     */
    private function createSynchro(array $synchroConfig)
    {
        if (!array_key_exists($key = 'name', $synchroConfig)) {
            throw ConfigException::missingKeyInImport($key);
        }
        $name = $synchroConfig['name'];

        if (!array_key_exists($key = 'source', $synchroConfig)) {
            throw ConfigException::missingKey($key);
            throw ConfigException::missingKeyInSynchro($key, $name);
        }
        if (!array_key_exists($key = 'destination', $synchroConfig)) {
            throw ConfigException::missingKey($key);
            throw ConfigException::missingKeyInSynchro($key, $name);
        }

        $source = $this->createSource($synchroConfig['source']);