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

namespace UnicaenAuth\Options;

5
6
7
use Assert\Assertion;
use Assert\AssertionFailedException;
use UnicaenApp\Exception\RuntimeException;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
 * Description of ModuleOptionsFactory
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class ModuleOptionsFactory implements FactoryInterface
{
    /**
     * Create service
     *
     * @param ServiceLocatorInterface $serviceLocator
     * @return mixed
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $config       = $serviceLocator->get('Configuration');
27
        $moduleConfig = isset($config['unicaen-auth']) ? $config['unicaen-auth'] : [];
28
        $moduleConfig = array_merge($config['zfcuser'], $moduleConfig);
29

30
31
        $this->validateConfig($moduleConfig);

32
33
        return new ModuleOptions($moduleConfig);
    }
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
65
66
67

    /**
     * @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.
        //

    }
68
}