Completed
Push — master ( 3a31a3...e3b547 )
by max
12:05
created

ConfigAbstractFactory::canCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace T4web\DomainModule\Infrastructure;
4
5
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
6
use Interop\Container\ContainerInterface;
7
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
8
use T4webInfrastructure\Config;
9
10
/**
11
 * Create Service by template:
12
 *   MODULE-NAME\ENTITY-NAME\Infrastructure\Config
13
 *
14
 * @package T4web\DomainModule\Infrastructure
15
 */
16
class ConfigAbstractFactory implements AbstractFactoryInterface
17
{
18
    public function canCreate(ContainerInterface $container, $requestedName)
19
    {
20
        return substr($requestedName, -strlen('Infrastructure\Config')) == 'Infrastructure\Config';
21
    }
22
23
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
24
    {
25
        $namespace = strstr($requestedName, 'Infrastructure\Config', true);
26
27
        $namespaceParts = explode('\\', trim($namespace, "\\"));
28
29
        if (count($namespaceParts) > 1) {
30
            $entityName = $namespaceParts[1];
31
        } else {
32
            $entityName = $namespaceParts[0];
33
        }
34
35
        $config = $container->get('Config');
36
37
        if (!isset($config['entity_map'])) {
38
            throw new ServiceNotCreatedException("You must define
39
                and configure $entityName in 'entity_map' config entry");
40
        }
41
42
        return new Config($config['entity_map']);
43
    }
44
}
45