Completed
Push — master ( 85ede1...125750 )
by max
08:18
created

createServiceWithName()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 22

Duplication

Lines 13
Ratio 40.63 %

Importance

Changes 0
Metric Value
dl 13
loc 32
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 22
nc 2
nop 3
1
<?php
2
3
namespace T4web\DomainModule\Infrastructure;
4
5
use Zend\ServiceManager\AbstractFactoryInterface;
6
use Zend\ServiceManager\ServiceLocatorInterface;
7
use T4webInfrastructure\InMemoryRepository;
8
use T4webInfrastructure\Config;
9
10
/**
11
 * Create Service by template:
12
 *   MODULE-NAME\ENTITY-NAME\Infrastructure\InMemoryRepository
13
 *
14
 * @package T4web\DomainModule\Infrastructure
15
 */
16
class InMemoryRepositoryAbstractFactory implements AbstractFactoryInterface
17
{
18
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceManager, $name, $requestedName)
19
    {
20
        return substr($requestedName, -strlen('Infrastructure\InMemoryRepository')) == 'Infrastructure\InMemoryRepository';
21
    }
22
23
    public function createServiceWithName(ServiceLocatorInterface $serviceManager, $name, $requestedName)
24
    {
25
        $namespace = strstr($requestedName, 'Infrastructure\InMemoryRepository', 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 = $serviceManager->get("$moduleName\\$entityName\\Infrastructure\\Config");
33
            $criteriaFactory = $serviceManager->get("$moduleName\\$entityName\\Infrastructure\\CriteriaFactory");
34
            $entityFactory = $serviceManager->get("$moduleName\\$entityName\\EntityFactory");
35
        } else {
36
            $entityName = $namespaceParts[0];
37
            /** @var Config $config */
38
            $config = $serviceManager->get("$entityName\\Infrastructure\\Config");
39
            $criteriaFactory = $serviceManager->get("$entityName\\Infrastructure\\CriteriaFactory");
40
            $entityFactory = $serviceManager->get("$entityName\\EntityFactory");
41
        }
42
43
        $eventManager = $serviceManager->get('EventManager');
44
        $eventManager->addIdentifiers("$entityName\\Infrastructure\\Repository");
45
        $collectionClass = $config->getCollectionClass($entityName);
46
47
        return new InMemoryRepository(
48
            $entityName,
49
            $collectionClass,
50
            $criteriaFactory,
0 ignored issues
show
Documentation introduced by
$criteriaFactory is of type object|array, but the function expects a object<T4webInfrastructure\CriteriaFactory>.

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...
51
            $entityFactory,
0 ignored issues
show
Documentation introduced by
$entityFactory is of type object|array, but the function expects a object<T4webDomainInterf...EntityFactoryInterface>.

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...
52
            $eventManager
0 ignored issues
show
Documentation introduced by
$eventManager is of type object|array, but the function expects a object<Zend\EventManager\EventManagerInterface>.

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...
53
        );
54
    }
55
}
56