1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace T4web\DomainModule\Service; |
4
|
|
|
|
5
|
|
|
use Zend\ServiceManager\Factory\AbstractFactoryInterface; |
6
|
|
|
use Interop\Container\ContainerInterface; |
7
|
|
|
use T4webDomain\Service\Creator; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Create Service by template: |
11
|
|
|
* MODULE-NAME\ENTITY-NAME\Service\Creator |
12
|
|
|
* |
13
|
|
|
* @package T4web\DomainModule\Service |
14
|
|
|
*/ |
15
|
|
|
class CreatorAbstractFactory implements AbstractFactoryInterface |
16
|
|
|
{ |
17
|
|
|
public function canCreate(ContainerInterface $container, $requestedName) |
18
|
|
|
{ |
19
|
|
|
return substr($requestedName, -strlen('Service\Creator')) == 'Service\Creator'; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
23
|
|
|
{ |
24
|
|
|
$namespace = strstr($requestedName, 'Service\Creator', true); |
25
|
|
|
|
26
|
|
|
$namespaceParts = explode('\\', trim($namespace, "\\")); |
27
|
|
|
|
28
|
|
View Code Duplication |
if (count($namespaceParts) > 1) { |
|
|
|
|
29
|
|
|
list($moduleName, $entityName) = $namespaceParts; |
30
|
|
|
$repository = $container->get("$moduleName\\$entityName\\Infrastructure\\Repository"); |
31
|
|
|
$entityFactory = $container->get("$moduleName\\$entityName\\EntityFactory"); |
32
|
|
|
$entityEventManager = $container->get("$moduleName\\$entityName\\EntityEventManager"); |
33
|
|
|
} else { |
34
|
|
|
$entityName = $namespaceParts[0]; |
35
|
|
|
$repository = $container->get("$entityName\\Infrastructure\\Repository"); |
36
|
|
|
$entityFactory = $container->get("$entityName\\EntityFactory"); |
37
|
|
|
$entityEventManager = $container->get("$entityName\\EntityEventManager"); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return new Creator($repository, $entityFactory, $entityEventManager); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
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.