ModuleOptionsFactory.php 1.68 KB
Newer Older
1
2
3
4
<?php

namespace UnicaenAuth\Options;

5
6
use Assert\Assertion;
use Assert\AssertionFailedException;
7
use Interop\Container\ContainerInterface;
8
use UnicaenApp\Exception\RuntimeException;
9
10
11
12

/**
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
13
class ModuleOptionsFactory
14
15
16
17
{
    /**
     * Create service
     *
18
     * @param ContainerInterface $container
19
20
     * @return mixed
     */
21
    public function __invoke(ContainerInterface $container)
22
    {
23
        $config       = $container->get('Configuration');
24
        $moduleConfig = isset($config['unicaen-auth']) ? $config['unicaen-auth'] : [];
25
        $moduleConfig = array_merge($config['zfcuser'], $moduleConfig);
26

27
28
        $this->validateConfig($moduleConfig);

29
30
        return new ModuleOptions($moduleConfig);
    }
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

    /**
     * @param array $config
     */
    private function validateConfig(array $config)
    {
        $configKeyPath = ['unicaen-auth'];

        //
        // Config shibboleth.
        //
        $parentKey = 'shibboleth';
        if (array_key_exists($parentKey, $config)) {
            $shibConfig = $config[$parentKey];
            $configKeyPath[] = $parentKey;

            try {
                Assertion::keyExists($shibConfig, $k = 'logout_url');
            } catch (AssertionFailedException $e) {
                throw new RuntimeException(sprintf(
                    "La clé de configuration '%s.$k' est absente (inspirez-vous du fichier de config " .
                    "unicaen-auth.global.php.dist du module unicaen/auth si besoin)",
                    join('.', $configKeyPath)
                ));
            }

            array_pop($configKeyPath);
        }

        //
        // Autres.
        //

    }
65
}