Completed
Push — master ( 111bcd...99ca87 )
by Andrey
26:30
created

EventDispatcherFactory::createService()   D

Complexity

Conditions 9
Paths 19

Size

Total Lines 38
Code Lines 25

Duplication

Lines 38
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 38
loc 38
rs 4.909
cc 9
eloc 25
nc 19
nop 1
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
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
Coding Style introduced by
Expected 1 space after class name; 2 found
Loading history...
Coding Style introduced by
Expected 1 space before implements keyword; 2 found
Loading history...
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