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

3
4
namespace UnicaenAuth\Authentication\Adapter;

5
use Interop\Container\ContainerInterface;
6
7
8
use UnicaenApp\Exception\LogicException;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
9
10
11
12
13
14
15
16
17
18
19
20
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

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

    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
25
26
27
28
29
30
31
32
33
34
    {
        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)
35
36
37
38
39
40
41
42
43
44
45
    {
        switch ($requestedName) {
            case __NAMESPACE__ . '\Ldap':
                $adapter = new Ldap();
                break;
            case __NAMESPACE__ . '\Db':
                $adapter = new Db();
                break;
            case __NAMESPACE__ . '\Cas':
                $adapter = new Cas();
                break;
46
47
48
49
50
51
            //
            // 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;
52
            default:
53
                throw new LogicException("Service demandé inattendu : '$requestedName'!");
54
55
                break;
        }
56

57
58
        if ($adapter instanceof EventManagerAwareInterface) {
            /** @var EventManager $eventManager */
59
            $eventManager = $container->get('event_manager');
60
            $adapter->setEventManager($eventManager);
61
            $userService = $container->get('unicaen-auth_user_service'); /* @var $userService \UnicaenAuth\Service\User */
62
            $eventManager->attach('userAuthenticated', [$userService, 'userAuthenticated'], 100);
63
        }
64

65
66
67
        return $adapter;
    }
}