Completed
Push — master ( 5857a4...4856e4 )
by max
02:36
created

createServiceWithName()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 57
Code Lines 37

Duplication

Lines 15
Ratio 26.32 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 15
loc 57
rs 8.7433
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\AbstractFactoryInterface;
8
use Zend\ServiceManager\ServiceLocatorInterface;
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 canCreateServiceWithName(ServiceLocatorInterface $serviceManager, $name, $requestedName)
22
    {
23
        return substr($requestedName, -strlen('Infrastructure\FinderAggregateRepository')) == 'Infrastructure\FinderAggregateRepository';
24
    }
25
26
    public function createServiceWithName(ServiceLocatorInterface $serviceManager, $name, $requestedName)
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 = $serviceManager->get("$moduleName\\$entityName\\Infrastructure\\Config");
36
            $repository = $serviceManager->get("$moduleName\\$entityName\\Infrastructure\\Repository");
37
            $mapper = $serviceManager->get("$moduleName\\$entityName\\Infrastructure\\Mapper");
38
            $entityFactory = $serviceManager->get("$moduleName\\$entityName\\EntityFactory");
39
        } else {
40
            $entityName = $namespaceParts[0];
41
            /** @var Config $config */
42
            $config = $serviceManager->get("$entityName\\Infrastructure\\Config");
43
            $repository = $serviceManager->get("$entityName\\Infrastructure\\Repository");
44
            $mapper = $serviceManager->get("$entityName\\Infrastructure\\Mapper");
45
            $entityFactory = $serviceManager->get("$entityName\\EntityFactory");
46
        }
47
48
        $appConfig = $serviceManager->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] = $serviceManager->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 = $serviceManager->get('Zend\Db\Adapter\Adapter');
71
72
        $tableGateway = new TableGateway($config->getTable($entityName), $dbAdapter, $features);
0 ignored issues
show
Documentation introduced by
$dbAdapter is of type object|array, but the function expects a object<Zend\Db\Adapter\AdapterInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
74
        return new FinderAggregateRepository(
75
            $tableGateway,
76
            $mapper,
77
            $entityFactory,
78
            $repository,
79
            $relatedRepository,
80
            $relationsConfig
81
        );
82
    }
83
}
84