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