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

createServiceWithName()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 33
Code Lines 18

Duplication

Lines 33
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 33
loc 33
rs 8.5806
cc 4
eloc 18
nc 4
nop 3
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/jms-serializer-module
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\JmsSerializerModule\ObjectConstructor;
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\Construction\ObjectConstructorInterface;
14
15
16
/**
17
 * Class ObjectConstructorAbstractFactory
18
 *
19
 * @package Nnx\JmsSerializerModule\ObjectConstructor
20
 */
21 View Code Duplication
class ObjectConstructorAbstractFactory 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...
22
{
23
    /**
24
     * @inheritdoc
25
     *
26
     * @param ServiceLocatorInterface $serviceLocator
27
     * @param                         $name
28
     * @param                         $requestedName
29
     *
30
     * @return bool|void
31
     */
32
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
33
    {
34
        return 0 === strpos($requestedName, 'nnxJmsSerializer.objectConstructor.');
35
    }
36
37
    /**
38
     * @inheritdoc
39
     *
40
     * @param ServiceLocatorInterface $serviceLocator
41
     * @param                         $name
42
     * @param                         $requestedName
43
     *
44
     * @return ObjectConstructorInterface
45
     * @throws \Nnx\JmsSerializerModule\ObjectConstructor\Exception\RuntimeException
46
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
47
     * @throws \Nnx\JmsSerializerModule\Options\Exception\InvalidArgumentException
48
     */
49
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
50
    {
51
        $appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() : $serviceLocator;
52
53
        $objectConstructorName = substr($requestedName, 35);
54
55
        /** @var  ModuleOptionsPluginManagerInterface $moduleOptionsManager */
56
        $moduleOptionsManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class);
57
58
        /** @var ModuleOptions $moduleOptions */
59
        $moduleOptions = $moduleOptionsManager->get(ModuleOptions::class);
60
61
        $objectConstructorConfig = $moduleOptions->getObjectConstructor($objectConstructorName);
62
63
        $name = $objectConstructorConfig->getName();
64
        $options = $objectConstructorConfig->getOptions();
65
66
        $objectConstructor =  $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 (!$objectConstructor instanceof ObjectConstructorInterface) {
72
            $errMsg = sprintf(
73
                'Object constructor of type %s is invalid; must implement %s',
74
                (is_object($objectConstructor) ? get_class($objectConstructor) : gettype($objectConstructor)),
75
                ObjectConstructorInterface::class
76
            );
77
            throw new Exception\RuntimeException($errMsg);
78
        }
79
80
        return $objectConstructor;
81
    }
82
}
83