AbstractFactory.php 4.71 KB
Newer Older
1
<?php
2

3 4
namespace UnicaenAuth\Authentication\Adapter;

5
use Interop\Container\ContainerInterface;
6
use UnicaenApp\Exception\LogicException;
7 8
use UnicaenAuth\Options\ModuleOptions;
use UnicaenAuth\Service\User;
9
use UnicaenAuth\Event\EventManager;
10
use Zend\EventManager\EventManagerAwareInterface;
11
use Zend\Router\Http\TreeRouteStack;
12 13
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
14
use UnicaenApp\Mapper\Ldap\People as LdapPeopleMapper;
15 16 17 18 19 20 21 22 23 24

/**
 * Description of AbstractFactory
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class AbstractFactory implements AbstractFactoryInterface
{
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
    {
25
        return $this->canCreate($serviceLocator, $requestedName);
26 27 28
    }

    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
29 30 31 32 33 34 35 36 37 38
    {
        return $this->__invoke($serviceLocator, $requestedName);
    }

    public function canCreate(ContainerInterface $container, $requestedName)
    {
        return strpos($requestedName, __NAMESPACE__) === 0 && class_exists($requestedName);
    }

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
39 40 41 42 43 44 45 46 47 48 49
    {
        switch ($requestedName) {
            case __NAMESPACE__ . '\Ldap':
                $adapter = new Ldap();
                break;
            case __NAMESPACE__ . '\Db':
                $adapter = new Db();
                break;
            case __NAMESPACE__ . '\Cas':
                $adapter = new Cas();
                break;
50 51 52 53 54 55
            //
            // NB: pour faire simple, la stratégie de créer un adapter pour l'auth Shibboleth n'a pas été retenue.
            //
            // case __NAMESPACE__ . '\Shib':
            //     $adapter = new Shib();
            //     break;
56
            default:
57
                throw new LogicException("Service demandé inattendu : '$requestedName'!");
58 59
                break;
        }
60

61 62
        $this->injectDependencies($adapter, $container);

63 64
        if ($adapter instanceof EventManagerAwareInterface) {
            /** @var EventManager $eventManager */
65
            $eventManager = $container->get(EventManager::class);
66
            $adapter->setEventManager($eventManager);
67
            $userService = $container->get('unicaen-auth_user_service'); /* @var $userService \UnicaenAuth\Service\User */
68
            $eventManager->attach('userAuthenticated', [$userService, 'userAuthenticated'], 100);
69 70 71
            $eventManager->attach('clear', function() use ($adapter){
                $adapter->getStorage()->clear();
            });
72
        }
73

74 75
        return $adapter;
    }
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

    /**
     * @param Ldap|Db|Cas        $adapter
     * @param ContainerInterface $container
     */
    private function injectDependencies($adapter, ContainerInterface $container)
    {
        switch (true) {

            case $adapter instanceof Ldap:
                /** @var User $userService */
                $userService = $container->get('unicaen-auth_user_service');
                $adapter->setUserService($userService);

                /** @var LdapPeopleMapper $ldapPeopleMapper */
                $ldapPeopleMapper = $container->get('ldap_people_mapper');
                $adapter->setLdapPeopleMapper($ldapPeopleMapper);

                $options = array_merge(
                    $container->get('zfcuser_module_options')->toArray(),
                    $container->get('unicaen-auth_module_options')->toArray());
                $adapter->setOptions(new ModuleOptions($options));

                /** @var \UnicaenApp\Options\ModuleOptions $appModuleOptions */
                $appModuleOptions = $container->get('unicaen-app_module_options');
                $adapter->setAppModuleOptions($appModuleOptions);

                break;

            case $adapter instanceof Cas:
                /** @var User $userService */
                $userService = $container->get('unicaen-auth_user_service');
                $adapter->setUserService($userService);

110
                /** @var mixed $router */
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
                $router = $container->get('router');
                $adapter->setRouter($router);

                $options = array_merge(
                    $container->get('zfcuser_module_options')->toArray(),
                    $container->get('unicaen-auth_module_options')->toArray());
                $adapter->setOptions(new ModuleOptions($options));

                /** @var LdapPeopleMapper $ldapPeopleMapper */
                $ldapPeopleMapper = $container->get('ldap_people_mapper');
                $adapter->setLdapPeopleMapper($ldapPeopleMapper);

                break;

            default:
                break;
        }
    }
129
}