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