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

3
4
namespace UnicaenAuth\Authentication\Storage;

5
use Interop\Container\ContainerInterface;
6
use UnicaenAuth\Options\ModuleOptions;
7
use UnicaenAuth\Event\EventManager;
8
9
10
11
12
13
14
15
16
17
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
 * Description of ChainAuthenticationStorageServiceFactory
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class ChainServiceFactory implements FactoryInterface
{
18
    private $mandatoryStorages = [
19
20
        200 => 'UnicaenAuth\Authentication\Storage\Ldap',
        100 => 'UnicaenAuth\Authentication\Storage\Db',
21
        75  => 'UnicaenAuth\Authentication\Storage\Shib',
22
23
    ];

24
25
    protected $storages = [];

26
    public function createService(ServiceLocatorInterface $serviceLocator)
27
28
29
30
31
    {
        return $this->__invoke($serviceLocator, '?');
    }

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
32
33
    {
        $chain = new Chain();
34

35
        /** @var EventManager $eventManager */
36
        $eventManager = $container->get(EventManager::class);
37
38
39

        $chain->setEventManager($eventManager);

40
        /** @var ModuleOptions $options */
41
        $options = $container->get('unicaen-auth_module_options');
42
43
44
45
46
47

        // retrait du fournisseur Ldap si l'auth LDAP est désactivée
        if (isset($options->getLdap()['enabled']) && ! $options->getLdap()['enabled']) {
            unset($this->mandatoryStorages[200]);
        }

48
49
50
51
        $storages = $this->mandatoryStorages + $this->storages;
        krsort($storages);

        foreach ($storages as $priority => $name) {
52
            $storage = $container->get($name);
53
54
55
            $chain->getEventManager()->attach('read', [$storage, 'read'], $priority);
            $chain->getEventManager()->attach('write', [$storage, 'write'], $priority);
            $chain->getEventManager()->attach('clear', [$storage, 'clear'], $priority);
56
        }
57

58
59
60
        return $chain;
    }
}