1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/jms-serializer-module |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\JmsSerializerModule\HandlerRegistry; |
7
|
|
|
|
8
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
9
|
|
|
use Zend\ServiceManager\FactoryInterface; |
10
|
|
|
use Zend\ServiceManager\MutableCreationOptionsInterface; |
11
|
|
|
use Zend\ServiceManager\MutableCreationOptionsTrait; |
12
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
13
|
|
|
use JMS\Serializer\Handler\HandlerRegistry; |
14
|
|
|
use ReflectionClass; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class HandlerRegistryFactory |
18
|
|
|
* |
19
|
|
|
* @package Nnx\JmsSerializerModule\HandlerRegistry |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
class HandlerRegistryFactory implements FactoryInterface, MutableCreationOptionsInterface |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
use MutableCreationOptionsTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
27
|
|
|
* |
28
|
|
|
* @return HandlerRegistry |
29
|
|
|
* @throws \Nnx\JmsSerializerModule\HandlerRegistry\Exception\RuntimeException |
30
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
31
|
|
|
* @throws \Nnx\JmsSerializerModule\MetadataDriver\Exception\RuntimeException |
32
|
|
|
*/ |
33
|
|
|
public function createService(ServiceLocatorInterface $serviceLocator) |
34
|
|
|
{ |
35
|
|
|
$handlerRegistry = new HandlerRegistry(); |
36
|
|
|
$creationOptions = $this->getCreationOptions(); |
37
|
|
|
|
38
|
|
|
$subscribers = []; |
39
|
|
|
if (array_key_exists('subscribers', $creationOptions)) { |
40
|
|
|
if (!is_array($creationOptions['subscribers'])) { |
41
|
|
|
$errMsg = 'Subscribers for handler registry is not array'; |
42
|
|
|
throw new Exception\RuntimeException($errMsg); |
43
|
|
|
} |
44
|
|
|
$subscribers = $creationOptions['subscribers']; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
foreach ($subscribers as $subscriberName) { |
48
|
|
|
$subscriber = null; |
49
|
|
|
if (is_string($subscriberName)) { |
50
|
|
|
if ($serviceLocator->has($subscriberName)) { |
51
|
|
|
$subscriber = $serviceLocator->get($subscriberName); |
52
|
|
|
} elseif (class_exists($subscriberName)) { |
53
|
|
|
$r = new ReflectionClass($subscriberName); |
54
|
|
|
$subscriber = $r->newInstance(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (!$subscriber instanceof SubscribingHandlerInterface) { |
59
|
|
|
$errMsg = sprintf( |
60
|
|
|
'Subscriber of type %s is invalid; must implement %s', |
61
|
|
|
(is_object($subscriber) ? get_class($subscriber) : gettype($subscriber)), |
62
|
|
|
SubscribingHandlerInterface::class |
63
|
|
|
); |
64
|
|
|
throw new Exception\RuntimeException($errMsg); |
65
|
|
|
} |
66
|
|
|
$handlerRegistry->registerSubscribingHandler($subscriber); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $handlerRegistry; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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.