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

FinderAggregateRepositoryFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 23.08 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 6
dl 15
loc 65
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A canCreate() 0 4 1
B __invoke() 15 57 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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