1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/jms-serializer-module |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\JmsSerializerModule\ObjectConstructor; |
7
|
|
|
|
8
|
|
|
use Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface; |
9
|
|
|
use Zend\ServiceManager\AbstractFactoryInterface; |
10
|
|
|
use Zend\ServiceManager\AbstractPluginManager; |
11
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
12
|
|
|
use Nnx\JmsSerializerModule\Options\ModuleOptions; |
13
|
|
|
use JMS\Serializer\Construction\ObjectConstructorInterface; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class ObjectConstructorAbstractFactory |
18
|
|
|
* |
19
|
|
|
* @package Nnx\JmsSerializerModule\ObjectConstructor |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
class ObjectConstructorAbstractFactory implements AbstractFactoryInterface |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @inheritdoc |
25
|
|
|
* |
26
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
27
|
|
|
* @param $name |
28
|
|
|
* @param $requestedName |
29
|
|
|
* |
30
|
|
|
* @return bool|void |
31
|
|
|
*/ |
32
|
|
|
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
33
|
|
|
{ |
34
|
|
|
return 0 === strpos($requestedName, 'nnxJmsSerializer.objectConstructor.'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdoc |
39
|
|
|
* |
40
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
41
|
|
|
* @param $name |
42
|
|
|
* @param $requestedName |
43
|
|
|
* |
44
|
|
|
* @return ObjectConstructorInterface |
45
|
|
|
* @throws \Nnx\JmsSerializerModule\ObjectConstructor\Exception\RuntimeException |
46
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
47
|
|
|
* @throws \Nnx\JmsSerializerModule\Options\Exception\InvalidArgumentException |
48
|
|
|
*/ |
49
|
|
|
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
50
|
|
|
{ |
51
|
|
|
$appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() : $serviceLocator; |
52
|
|
|
|
53
|
|
|
$objectConstructorName = substr($requestedName, 35); |
54
|
|
|
|
55
|
|
|
/** @var ModuleOptionsPluginManagerInterface $moduleOptionsManager */ |
56
|
|
|
$moduleOptionsManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class); |
57
|
|
|
|
58
|
|
|
/** @var ModuleOptions $moduleOptions */ |
59
|
|
|
$moduleOptions = $moduleOptionsManager->get(ModuleOptions::class); |
60
|
|
|
|
61
|
|
|
$objectConstructorConfig = $moduleOptions->getObjectConstructor($objectConstructorName); |
62
|
|
|
|
63
|
|
|
$name = $objectConstructorConfig->getName(); |
64
|
|
|
$options = $objectConstructorConfig->getOptions(); |
65
|
|
|
|
66
|
|
|
$objectConstructor = $serviceLocator->get( |
67
|
|
|
$name, |
68
|
|
|
$options |
|
|
|
|
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
if (!$objectConstructor instanceof ObjectConstructorInterface) { |
72
|
|
|
$errMsg = sprintf( |
73
|
|
|
'Object constructor of type %s is invalid; must implement %s', |
74
|
|
|
(is_object($objectConstructor) ? get_class($objectConstructor) : gettype($objectConstructor)), |
75
|
|
|
ObjectConstructorInterface::class |
76
|
|
|
); |
77
|
|
|
throw new Exception\RuntimeException($errMsg); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $objectConstructor; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
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.