AbstractFactory.php 2.11 KB
Newer Older
1
2
3
4
5
6
7
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace UnicaenAuth\Authentication\Adapter;

use UnicaenApp\Exception;
use UnicaenAuth\Authentication\Adapter\Cas;
use UnicaenAuth\Authentication\Adapter\Db;
use UnicaenAuth\Authentication\Adapter\Ldap;
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
     * @return mixed
     */
    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;
            default:
                throw new Exception("Service demandé inattendu : '$requestedName'!");
                break;
        }
        
        if ($adapter instanceof \Zend\EventManager\EventManagerAwareInterface) {
57
58
            $eventManager = $serviceLocator->get('event_manager');
            $adapter->setEventManager($eventManager);
59
60
            $userService = $serviceLocator->get('unicaen-auth_user_service'); /* @var $userService \UnicaenAuth\Service\User */
            $eventManager->attach('userAuthenticated', array($userService, 'userAuthenticated'), 100);
61
62
63
64
65
        }
        
        return $adapter;
    }
}