ChainServiceFactory.php 1.96 KB
Newer Older
1
<?php
2

3
4
namespace UnicaenAuth\Provider\Identity;

5
use Interop\Container\ContainerInterface;
6
7
8
9
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
10
 * Intsancie une chaîne de fournisseurs d'identité.
11
12
13
14
15
16
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class ChainServiceFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
17
18
19
20
21
    {
        return $this->__invoke($serviceLocator, '?');
    }

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
22
23
    {
        $chain = new Chain();
24
        $chain->setServiceLocator($container);
25

26
        $providers = $this->computeProviders($container);
27

28
        foreach ($providers as $priority => $name) {
29
            $provider = $container->get($name);
30
            $chain->getEventManager()->attach('getIdentityRoles', [$provider, 'injectIdentityRoles'], $priority);
31
        }
32

33
34
        return $chain;
    }
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

    /**
     * @param ServiceLocatorInterface $serviceLocator
     * @return array
     */
    private function computeProviders(ServiceLocatorInterface $serviceLocator)
    {
        $config = $serviceLocator->get('Config'); //'unicaen-auth_module_options'
        if (! isset($config['unicaen-auth']['identity_providers']) || ! $config['unicaen-auth']['identity_providers']) {
            throw new \UnicaenApp\Exception\RuntimeException("Aucun fournisseur d'identité spécifié dans la config.");
        }

        $providers = (array) $config['unicaen-auth']['identity_providers'];

        // retrait du fournisseur 'Ldap' si l'auth Ldap est désactivée
        if (isset($config['unicaen-auth']['ldap']['enabled']) && ! $config['unicaen-auth']['ldap']['enabled']) {
            $key = array_search('UnicaenAuth\Provider\Identity\Ldap', $providers, true);
            if ($key !== false) {
                unset($providers[$key]);
            }
        }

        return $providers;
    }
59
}