DbRoleServiceFactory.php 1.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

namespace UnicaenAuth\Provider\Role;

use BjyAuthorize\Exception\InvalidArgumentException;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
10
 * Factory responsible of instantiating {@see \UnicaenAuth\Provider\Role\DbRole}
11
 */
12
class DbRoleServiceFactory implements FactoryInterface
13
14
15
16
{
    /**
     * {@inheritDoc}
     *
17
     * @return DbRole
18
19
20
21
22
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $config = $serviceLocator->get('BjyAuthorize\Config');

23
        if (! isset($config['role_providers']['UnicaenAuth\Provider\Role\DbRole'])) {
24
            throw new InvalidArgumentException(
25
                'Config for "UnicaenAuth\Provider\Role\DbRole" not set'
26
27
28
            );
        }

29
        $providerConfig = $config['role_providers']['UnicaenAuth\Provider\Role\DbRole'];
30
31
32
33
34
35
36
37
38
39
40
41

        if (! isset($providerConfig['role_entity_class'])) {
            throw new InvalidArgumentException('role_entity_class not set in the bjyauthorize role_providers config.');
        }

        if (! isset($providerConfig['object_manager'])) {
            throw new InvalidArgumentException('object_manager not set in the bjyauthorize role_providers config.');
        }

        /* @var $objectManager \Doctrine\Common\Persistence\ObjectManager */
        $objectManager = $serviceLocator->get($providerConfig['object_manager']);

42
        return new DbRole($objectManager->getRepository($providerConfig['role_entity_class']));
43
44
    }
}