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

DeleterAbstractFactory::canCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
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\Deleter;
8
9
/**
10
 * Create Service by template:
11
 *   MODULE-NAME\ENTITY-NAME\Service\Deleter
12
 *
13
 * @package T4web\DomainModule\Service
14
 */
15 View Code Duplication
class DeleterAbstractFactory implements AbstractFactoryInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
16
{
17
    public function canCreate(ContainerInterface $container, $requestedName)
18
    {
19
        return substr($requestedName, -strlen('Service\Deleter')) == 'Service\Deleter';
20
    }
21
22
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
23
    {
24
        $namespace = strstr($requestedName, 'Service\Deleter', true);
25
26
        $namespaceParts = explode('\\', trim($namespace, "\\"));
27
28
        if (count($namespaceParts) > 1) {
29
            list($moduleName, $entityName) = $namespaceParts;
30
            $repository = $container->get("$moduleName\\$entityName\\Infrastructure\\Repository");
31
            $entityEventManager = $container->get("$moduleName\\$entityName\\EntityEventManager");
32
        } else {
33
            $entityName = $namespaceParts[0];
34
            $repository = $container->get("$entityName\\Infrastructure\\Repository");
35
            $entityEventManager = $container->get("$entityName\\EntityEventManager");
36
        }
37
38
        return new Deleter($repository, $entityEventManager);
39
    }
40
}
41