ChainServiceFactory.php 1.25 KB
Newer Older
1
2
3
4
5
6
7
<?php
namespace UnicaenAuth\Provider\Identity;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
8
 * Intsancie une chaîne de fournisseurs d'identité.
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class ChainServiceFactory implements FactoryInterface
{
    /**
     * Create service
     *
     * @param ServiceLocatorInterface $serviceLocator
     * @return mixed
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
22
23
24
25
        $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.");
        }
26

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

29
30
        $chain = new Chain();
        $chain->setServiceLocator($serviceLocator);
31

32
33
        foreach ($providers as $priority => $name) {
            $provider = $serviceLocator->get($name);
34
            $chain->getEventManager()->attach('getIdentityRoles', [$provider, 'injectIdentityRoles'], $priority);
35
        }
36

37
38
39
        return $chain;
    }
}