ConfigAbstractFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A canCreate() 0 4 1
A __invoke() 0 21 3
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