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

createServiceWithName()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 57
Code Lines 37

Duplication

Lines 15
Ratio 26.32 %

Importance

Changes 0
Metric Value
dl 15
loc 57
rs 8.7433
c 0
b 0
f 0
cc 6
eloc 37
nc 10
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace T4web\DomainModule\Infrastructure;
4
5
use Zend\Db\TableGateway\TableGateway;
6
use Zend\Db\TableGateway\Feature\SequenceFeature;
7
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
8
use Interop\Container\ContainerInterface;
9
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
10
use T4webInfrastructure\Config;
11
use T4webInfrastructure\FinderAggregateRepository;
12
13
/**
14
 * Create Service by template:
15
 *   MODULE-NAME\ENTITY-NAME\Infrastructure\FinderAggregateRepository
16
 *
17
 * @package T4web\DomainModule\Infrastructure
18
 */
19
class FinderAggregateRepositoryFactory implements AbstractFactoryInterface
20
{
21
    public function canCreate(ContainerInterface $container, $requestedName)
22
    {
23
        return substr($requestedName, -strlen('Infrastructure\FinderAggregateRepository')) == 'Infrastructure\FinderAggregateRepository';
24
    }
25
26
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
27
    {
28
        $namespace = strstr($requestedName, 'Infrastructure\FinderAggregateRepository', true);
29
30
        $namespaceParts = explode('\\', trim($namespace, "\\"));
31
32 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...
33
            list($moduleName, $entityName) = $namespaceParts;
34
            /** @var Config $config */
35
            $config = $container->get("$moduleName\\$entityName\\Infrastructure\\Config");
36
            $repository = $container->get("$moduleName\\$entityName\\Infrastructure\\Repository");
37
            $mapper = $container->get("$moduleName\\$entityName\\Infrastructure\\Mapper");
38
            $entityFactory = $container->get("$moduleName\\$entityName\\EntityFactory");
39
        } else {
40
            $entityName = $namespaceParts[0];
41
            /** @var Config $config */
42
            $config = $container->get("$entityName\\Infrastructure\\Config");
43
            $repository = $container->get("$entityName\\Infrastructure\\Repository");
44
            $mapper = $container->get("$entityName\\Infrastructure\\Mapper");
45
            $entityFactory = $container->get("$entityName\\EntityFactory");
46
        }
47
48
        $appConfig = $container->get('Config');
49
50
        if (!isset($appConfig['entity_map'])) {
51
            throw new ServiceNotCreatedException("You must define
52
                and configure Comments\\Comment in 'entity_map' config entry");
53
        }
54
55
        $relationsConfig = $appConfig['entity_map'][$entityName]['relations'];
56
57
        $relatedRepository = [];
58
        foreach ($relationsConfig as $relatedEntity => $joinRule) {
59
            $repositoryClass = $relatedEntity . '\Infrastructure\Repository';
60
            $relatedRepository[$relatedEntity] = $container->get($repositoryClass);
61
        }
62
63
        $features = [];
64
        $tableSequence = $config->getSequence($entityName);
65
        $tablePrimaryKey = $config->getPrimaryKey($entityName);
66
        if (!empty($tableSequence) && !empty($tablePrimaryKey)) {
67
            $features[] = new SequenceFeature($tablePrimaryKey, $tableSequence);
68
        }
69
70
        $dbAdapter = $container->get('Zend\Db\Adapter\Adapter');
71
72
        $tableGateway = new TableGateway($config->getTable($entityName), $dbAdapter, $features);
73
74
        return new FinderAggregateRepository(
75
            $tableGateway,
76
            $mapper,
77
            $entityFactory,
78
            $repository,
79
            $relatedRepository,
80
            $relationsConfig
81
        );
82
    }
83
}
84