Completed
Push — master ( 85fa2d...2b32c0 )
by Dmitriy
02:26
created

RepositoryAbstractFactory::createServiceWithName()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 3
Metric Value
c 4
b 0
f 3
dl 0
loc 31
rs 8.8571
cc 3
eloc 20
nc 2
nop 3
1
<?php
2
3
namespace T4web\DomainModule\Infrastructure;
4
5
use Zend\Db\TableGateway\TableGateway;
6
use Zend\ServiceManager\AbstractFactoryInterface;
7
use Zend\ServiceManager\ServiceLocatorInterface;
8
use T4webInfrastructure\Repository;
9
use T4webInfrastructure\Config;
10
11
/**
12
 * Create Service by template:
13
 *   MODULE-NAME\ENTITY-NAME\Infrastructure\Repository
14
 *
15
 * @package T4web\DomainModule\Infrastructure
16
 */
17
class RepositoryAbstractFactory implements AbstractFactoryInterface
18
{
19
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceManager, $name, $requestedName)
20
    {
21
        return substr($requestedName, -strlen('Infrastructure\Repository')) == 'Infrastructure\Repository';
22
    }
23
24
    public function createServiceWithName(ServiceLocatorInterface $serviceManager, $name, $requestedName)
25
    {
26
        $namespace = strstr($requestedName, 'Infrastructure\Repository', true);
27
28
        list($moduleName, $entityName) = explode('\\', $namespace);
29
30
        $dbAdapter = $serviceManager->get('Zend\Db\Adapter\Adapter');
31
        /** @var Config $config */
32
        $config = $serviceManager->get("$moduleName\\$entityName\\Infrastructure\\Config");
33
34
        $features = [];
35
        $tableSequence = $config->getSequence($entityName);
0 ignored issues
show
Bug introduced by
The method getSequence() does not seem to exist on object<T4webInfrastructure\Config>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
        $tablePrimaryKey = $config->getPrimaryKey($entityName);
0 ignored issues
show
Bug introduced by
The method getPrimaryKey() does not seem to exist on object<T4webInfrastructure\Config>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
        if (!empty($tableSequence) && !empty($tablePrimaryKey)) {
38
            $features[] = new SequenceFeature($tablePrimaryKey, $tableSequence);
39
        }
40
41
        $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...
42
43
        $eventManager = $serviceManager->get('EventManager');
44
        $eventManager->addIdentifiers($requestedName);
45
46
        return new Repository(
47
            $entityName,
48
            $serviceManager->get("$moduleName\\$entityName\\Infrastructure\\CriteriaFactory"),
0 ignored issues
show
Documentation introduced by
$serviceManager->get("{$...ture\\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...
49
            $tableGateway,
50
            $serviceManager->get("$moduleName\\$entityName\\Infrastructure\\Mapper"),
0 ignored issues
show
Documentation introduced by
$serviceManager->get("{$...nfrastructure\\Mapper") is of type object|array, but the function expects a object<T4webInfrastructure\Mapper>.

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
            $config,
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
}