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

CreatorAbstractFactory::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\Service;
4
5
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
6
use Interop\Container\ContainerInterface;
7
use T4webDomain\Service\Creator;
8
9
/**
10
 * Create Service by template:
11
 *   MODULE-NAME\ENTITY-NAME\Service\Creator
12
 *
13
 * @package T4web\DomainModule\Service
14
 */
15
class CreatorAbstractFactory implements AbstractFactoryInterface
16
{
17
    public function canCreate(ContainerInterface $container, $requestedName)
18
    {
19
        return substr($requestedName, -strlen('Service\Creator')) == 'Service\Creator';
20
    }
21
22
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
23
    {
24
        $namespace = strstr($requestedName, 'Service\Creator', true);
25
26
        $namespaceParts = explode('\\', trim($namespace, "\\"));
27
28 View Code Duplication
        if (count($namespaceParts) > 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
            list($moduleName, $entityName) = $namespaceParts;
30
            $repository = $container->get("$moduleName\\$entityName\\Infrastructure\\Repository");
31
            $entityFactory = $container->get("$moduleName\\$entityName\\EntityFactory");
32
            $entityEventManager = $container->get("$moduleName\\$entityName\\EntityEventManager");
33
        } else {
34
            $entityName = $namespaceParts[0];
35
            $repository = $container->get("$entityName\\Infrastructure\\Repository");
36
            $entityFactory = $container->get("$entityName\\EntityFactory");
37
            $entityEventManager = $container->get("$entityName\\EntityEventManager");
38
        }
39
40
        return new Creator($repository, $entityFactory, $entityEventManager);
41
    }
42
}
43