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

canCreateServiceWithName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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\Serializer;
7
8
use Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface;
9
use Zend\Serializer\Adapter\AdapterInterface;
10
use Zend\ServiceManager\AbstractFactoryInterface;
11
use Zend\ServiceManager\AbstractPluginManager;
12
use Zend\ServiceManager\ServiceLocatorInterface;
13
use Nnx\JmsSerializerModule\Options\ModuleOptions;
14
15
/**
16
 * Class JmsSerializerAbstractFactory
17
 *
18
 * @package Nnx\JmsSerializerModule\Serializer
19
 */
20
class JmsSerializerAbstractFactory implements AbstractFactoryInterface
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.serializers.');
34
    }
35
36
    /**
37
     * @inheritdoc
38
     *
39
     * @param ServiceLocatorInterface $serviceLocator
40
     * @param                         $name
41
     * @param                         $requestedName
42
     *
43
     * @return AdapterInterface
44
     * @throws \Nnx\JmsSerializerModule\Options\Exception\InvalidArgumentException
45
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
46
     */
47
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
48
    {
49
        $appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() : $serviceLocator;
50
51
        $jmsSerializerName = substr($requestedName, 29);
52
53
        /** @var  ModuleOptionsPluginManagerInterface $moduleOptionsManager */
54
        $moduleOptionsManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class);
55
56
        /** @var ModuleOptions $moduleOptions */
57
        $moduleOptions = $moduleOptionsManager->get(ModuleOptions::class);
58
59
        $serializerConfig = $moduleOptions->getSerializer($jmsSerializerName);
60
61
        $options = $serializerConfig->getOptions();
62
        $serializer = $serviceLocator->get(
0 ignored issues
show
Bug Compatibility introduced by
The expression $serviceLocator->get($se...ons->getTypeParser())); of type object|array adds the type array to the return on line 76 which is incompatible with the return type documented by Nnx\JmsSerializerModule\...::createServiceWithName of type Zend\Serializer\Adapter\AdapterInterface.
Loading history...
63
            $serializerConfig->getName(),
64
            [
0 ignored issues
show
Unused Code introduced by
The call to ServiceLocatorInterface::get() has too many arguments starting with array('metadataFactory' ...tions->getTypeParser()).

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...
65
                'metadataFactory' => $options->getMetadataFactory(),
66
                'handlerRegistry' => $options->getHandlerRegistry(),
67
                'objectConstructor' => $options->getObjectConstructor(),
68
                'serializationVisitors' => $options->getSerializationVisitors(),
69
                'deserializationVisitors' => $options->getDeserializationVisitors(),
70
                'eventDispatcher'  => $options->getEventDispatcher(),
71
                'typeParser' => $options->getTypeParser()
72
            ]
73
        );
74
75
76
        return $serializer;
77
    }
78
}
79