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

HandlerRegistryFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 0
cbo 4
dl 51
loc 51
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D createService() 38 38 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
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...
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