MapperAbstractFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 31.03 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 9
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A canCreate() 0 4 1
A __invoke() 9 21 2

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\ServiceManager\Factory\AbstractFactoryInterface;
6
use Interop\Container\ContainerInterface;
7
use T4webInfrastructure\Mapper;
8
use T4webInfrastructure\Config;
9
10
/**
11
 * Create Service by template:
12
 *   MODULE-NAME\ENTITY-NAME\Infrastructure\Mapper
13
 *
14
 * @package T4web\DomainModule\Infrastructure
15
 */
16
class MapperAbstractFactory implements AbstractFactoryInterface
17
{
18
    public function canCreate(ContainerInterface $container, $requestedName)
19
    {
20
        return substr($requestedName, -strlen('Infrastructure\Mapper')) == 'Infrastructure\Mapper';
21
    }
22
23
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
24
    {
25
        $namespace = strstr($requestedName, 'Infrastructure\Mapper', true);
26
27
        $namespaceParts = explode('\\', trim($namespace, "\\"));
28
29 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...
30
            list($moduleName, $entityName) = $namespaceParts;
31
            /** @var Config $config */
32
            $config = $container->get("$moduleName\\$entityName\\Infrastructure\\Config");
33
        } else {
34
            $entityName = $namespaceParts[0];
35
            /** @var Config $config */
36
            $config = $container->get("$entityName\\Infrastructure\\Config");
37
        }
38
39
        return new Mapper(
40
            $config->getColumnsAsAttributesMap($entityName),
41
            $config->getSerializedColumns($entityName)
42
        );
43
    }
44
}
45