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

3
4
namespace UnicaenAuth\Authentication\Adapter;

5
6
7
use UnicaenApp\Exception\LogicException;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
 * Description of AbstractFactory
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class AbstractFactory implements AbstractFactoryInterface
{
    /**
     * Determine if we can create a service with name
     *
     * @param ServiceLocatorInterface $serviceLocator
     * @param $name
     * @param $requestedName
     * @return bool
     */
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
    {
        return strpos($requestedName, __NAMESPACE__) === 0 && class_exists($requestedName);
    }

    /**
     * Create service with name
     *
     * @param ServiceLocatorInterface $serviceLocator
     * @param $name
     * @param $requestedName
37
     * @return \ZfcUser\Authentication\Adapter\AbstractAdapter
38
39
40
41
42
43
44
45
46
47
48
49
50
     */
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
    {
        switch ($requestedName) {
            case __NAMESPACE__ . '\Ldap':
                $adapter = new Ldap();
                break;
            case __NAMESPACE__ . '\Db':
                $adapter = new Db();
                break;
            case __NAMESPACE__ . '\Cas':
                $adapter = new Cas();
                break;
51
52
53
54
55
56
            //
            // 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;
57
            default:
58
                throw new LogicException("Service demandé inattendu : '$requestedName'!");
59
60
                break;
        }
61

62
63
        if ($adapter instanceof EventManagerAwareInterface) {
            /** @var EventManager $eventManager */
64
65
            $eventManager = $serviceLocator->get('event_manager');
            $adapter->setEventManager($eventManager);
66
            $userService = $serviceLocator->get('unicaen-auth_user_service'); /* @var $userService \UnicaenAuth\Service\User */
67
            $eventManager->attach('userAuthenticated', [$userService, 'userAuthenticated'], 100);
68
        }
69

70
71
72
        return $adapter;
    }
}