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 T4webInfrastructure\Repository; |
10
|
|
|
use T4webInfrastructure\Config; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Create Service by template: |
14
|
|
|
* MODULE-NAME\ENTITY-NAME\Infrastructure\Repository |
15
|
|
|
* |
16
|
|
|
* @package T4web\DomainModule\Infrastructure |
17
|
|
|
*/ |
18
|
|
|
class RepositoryAbstractFactory implements AbstractFactoryInterface |
19
|
|
|
{ |
20
|
|
|
public function canCreate(ContainerInterface $container, $requestedName) |
21
|
|
|
{ |
22
|
|
|
return substr($requestedName, -strlen('Infrastructure\Repository')) == 'Infrastructure\Repository'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
26
|
|
|
{ |
27
|
|
|
$namespace = strstr($requestedName, 'Infrastructure\Repository', true); |
28
|
|
|
|
29
|
|
|
$namespaceParts = explode('\\', trim($namespace, "\\")); |
30
|
|
|
|
31
|
|
View Code Duplication |
if (count($namespaceParts) > 1) { |
|
|
|
|
32
|
|
|
list($moduleName, $entityName) = $namespaceParts; |
33
|
|
|
/** @var Config $config */ |
34
|
|
|
$config = $container->get("$moduleName\\$entityName\\Infrastructure\\Config"); |
35
|
|
|
$criteriaFactory = $container->get("$moduleName\\$entityName\\Infrastructure\\CriteriaFactory"); |
36
|
|
|
$mapper = $container->get("$moduleName\\$entityName\\Infrastructure\\Mapper"); |
37
|
|
|
$entityFactory = $container->get("$moduleName\\$entityName\\EntityFactory"); |
38
|
|
|
} else { |
39
|
|
|
$entityName = $namespaceParts[0]; |
40
|
|
|
/** @var Config $config */ |
41
|
|
|
$config = $container->get("$entityName\\Infrastructure\\Config"); |
42
|
|
|
$criteriaFactory = $container->get("$entityName\\Infrastructure\\CriteriaFactory"); |
43
|
|
|
$mapper = $container->get("$entityName\\Infrastructure\\Mapper"); |
44
|
|
|
$entityFactory = $container->get("$entityName\\EntityFactory"); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$features = []; |
48
|
|
|
$tableSequence = $config->getSequence($entityName); |
49
|
|
|
$tablePrimaryKey = $config->getPrimaryKey($entityName); |
50
|
|
|
if (!empty($tableSequence) && !empty($tablePrimaryKey)) { |
51
|
|
|
$features[] = new SequenceFeature($tablePrimaryKey, $tableSequence); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$dbAdapter = $container->get('Zend\Db\Adapter\Adapter'); |
55
|
|
|
$tableGateway = new TableGateway($config->getTable($entityName), $dbAdapter, $features); |
56
|
|
|
|
57
|
|
|
$eventManager = $container->get('EventManager'); |
58
|
|
|
$eventManager->addIdentifiers([$requestedName]); |
59
|
|
|
|
60
|
|
|
return new Repository( |
61
|
|
|
$entityName, |
62
|
|
|
$criteriaFactory, |
63
|
|
|
$tableGateway, |
64
|
|
|
$mapper, |
65
|
|
|
$entityFactory, |
66
|
|
|
$eventManager, |
67
|
|
|
$tablePrimaryKey |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
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.