1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace T4web\DomainModule\Infrastructure; |
4
|
|
|
|
5
|
|
|
use Zend\ServiceManager\Factory\AbstractFactoryInterface; |
6
|
|
|
use Interop\Container\ContainerInterface; |
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 canCreate(ContainerInterface $container, $requestedName) |
19
|
|
|
{ |
20
|
|
|
return substr($requestedName, -strlen('Infrastructure\InMemoryRepository')) == 'Infrastructure\InMemoryRepository'; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
24
|
|
|
{ |
25
|
|
|
$namespace = strstr($requestedName, 'Infrastructure\InMemoryRepository', true); |
26
|
|
|
|
27
|
|
|
$namespaceParts = explode('\\', trim($namespace, "\\")); |
28
|
|
|
|
29
|
|
View Code Duplication |
if (count($namespaceParts) > 1) { |
|
|
|
|
30
|
|
|
list($moduleName, $entityName) = $namespaceParts; |
31
|
|
|
/** @var Config $config */ |
32
|
|
|
$config = $container->get("$moduleName\\$entityName\\Infrastructure\\Config"); |
33
|
|
|
$criteriaFactory = $container->get("$moduleName\\$entityName\\Infrastructure\\CriteriaFactory"); |
34
|
|
|
$entityFactory = $container->get("$moduleName\\$entityName\\EntityFactory"); |
35
|
|
|
} else { |
36
|
|
|
$entityName = $namespaceParts[0]; |
37
|
|
|
/** @var Config $config */ |
38
|
|
|
$config = $container->get("$entityName\\Infrastructure\\Config"); |
39
|
|
|
$criteriaFactory = $container->get("$entityName\\Infrastructure\\CriteriaFactory"); |
40
|
|
|
$entityFactory = $container->get("$entityName\\EntityFactory"); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$eventManager = $container->get('EventManager'); |
44
|
|
|
$eventManager->addIdentifiers(["$entityName\\Infrastructure\\Repository"]); |
45
|
|
|
$collectionClass = $config->getCollectionClass($entityName); |
46
|
|
|
|
47
|
|
|
return new InMemoryRepository( |
48
|
|
|
$entityName, |
49
|
|
|
$collectionClass, |
50
|
|
|
$criteriaFactory, |
51
|
|
|
$entityFactory, |
52
|
|
|
$eventManager |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
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.