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

HandlerRegistryAbstractFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 6
dl 63
loc 63
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A canCreateServiceWithName() 4 4 1
B createServiceWithName() 33 33 4

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 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\Handler\HandlerRegistryInterface;
14
15
/**
16
 * Class HandlerRegistryAbstractFactory
17
 *
18
 * @package Nnx\JmsSerializerModule\HandlerRegistry
19
 */
20 View Code Duplication
class HandlerRegistryAbstractFactory implements AbstractFactoryInterface
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...
21
{
22
    /**
23
     * @inheritdoc
24
     *
25
     * @param ServiceLocatorInterface $serviceLocator
26
     * @param                         $name
27
     * @param                         $requestedName
28
     *
29
     * @return bool|void
30
     */
31
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
32
    {
33
        return 0 === strpos($requestedName, 'nnxJmsSerializer.handlerRegistry.');
34
    }
35
36
    /**
37
     * @inheritdoc
38
     *
39
     * @param ServiceLocatorInterface $serviceLocator
40
     * @param                         $name
41
     * @param                         $requestedName
42
     *
43
     * @return HandlerRegistryInterface
44
     * @throws \Nnx\JmsSerializerModule\HandlerRegistry\Exception\RuntimeException
45
     * @throws \Nnx\JmsSerializerModule\MetadataDriver\Exception\RuntimeException
46
     * @throws \Nnx\JmsSerializerModule\Options\Exception\InvalidArgumentException
47
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
48
     */
49
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
50
    {
51
        $appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() : $serviceLocator;
52
53
        $handlerRegistryName = substr($requestedName, 33);
54
55
        /** @var  ModuleOptionsPluginManagerInterface $moduleOptionsManager */
56
        $moduleOptionsManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class);
57
58
        /** @var ModuleOptions $moduleOptions */
59
        $moduleOptions = $moduleOptionsManager->get(ModuleOptions::class);
60
61
        $handlerRegistryConfig = $moduleOptions->getHandlerRegistry($handlerRegistryName);
62
63
        $name = $handlerRegistryConfig->getName();
64
        $options = $handlerRegistryConfig->getOptions();
65
66
        $handlerRegistry =  $serviceLocator->get(
67
            $name,
68
            $options
0 ignored issues
show
Unused Code introduced by
The call to ServiceLocatorInterface::get() has too many arguments starting with $options.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
69
        );
70
71
        if (!$handlerRegistry instanceof HandlerRegistryInterface) {
72
            $errMsg = sprintf(
73
                'Handler registry of type %s is invalid; must implement %s',
74
                (is_object($handlerRegistry) ? get_class($handlerRegistry) : gettype($handlerRegistry)),
75
                HandlerRegistryInterface::class
76
            );
77
            throw new Exception\RuntimeException($errMsg);
78
        }
79
80
        return $handlerRegistry;
81
    }
82
}
83