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

3
4
namespace UnicaenAuth\Authentication\Storage;

5
use UnicaenAuth\Options\ModuleOptions;
6
7
8
9
10
11
12
13
14
15
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

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

22
23
    protected $storages = [];

24
25
26
27
28
29
30
31
32
    /**
     * Create service
     *
     * @param ServiceLocatorInterface $serviceLocator
     * @return mixed
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $chain = new Chain();
33

34
35
36
37
38
39
40
41
        /** @var ModuleOptions $options */
        $options = $serviceLocator->get('unicaen-auth_module_options');

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

42
43
44
45
        $storages = $this->mandatoryStorages + $this->storages;
        krsort($storages);

        foreach ($storages as $priority => $name) {
46
            $storage = $serviceLocator->get($name);
47
48
49
            $chain->getEventManager()->attach('read', [$storage, 'read'], $priority);
            $chain->getEventManager()->attach('write', [$storage, 'write'], $priority);
            $chain->getEventManager()->attach('clear', [$storage, 'clear'], $priority);
50
        }
51

52
53
54
        return $chain;
    }
}