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

MetadataAbstractFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A canCreateServiceWithName() 0 4 1
B createServiceWithName() 0 32 2
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/jms-serializer-module
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\JmsSerializerModule\MetadataFactory;
7
8
use Nnx\JmsSerializerModule\Options\ModuleOptions;
9
use Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface;
10
use Zend\ServiceManager\AbstractFactoryInterface;
11
use Zend\ServiceManager\AbstractPluginManager;
12
use Zend\ServiceManager\ServiceLocatorInterface;
13
use ReflectionClass;
14
use Metadata\Driver\DriverInterface;
15
16
/**
17
 * Class MetadataAbstractFactory
18
 *
19
 * @package Nnx\JmsSerializerModule\MetadataFactory
20
 */
21
class MetadataAbstractFactory implements AbstractFactoryInterface
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.metadataFactory.');
35
    }
36
37
    /**
38
     * @inheritdoc
39
     *
40
     * @param ServiceLocatorInterface $serviceLocator
41
     * @param                         $name
42
     * @param                         $requestedName
43
     *
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
        $metadataFactoryName = substr($requestedName, 33);
52
53
        /** @var  ModuleOptionsPluginManagerInterface $moduleOptionsManager */
54
        $moduleOptionsManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class);
55
56
        /** @var ModuleOptions $moduleOptions */
57
        $moduleOptions = $moduleOptionsManager->get(ModuleOptions::class);
58
59
        $metadataFactoryConfig = $moduleOptions->gasMetadataFactory($metadataFactoryName);
60
61
        $factoryClassName = $metadataFactoryConfig->getName();
62
        $metadataFactoryOptions = $metadataFactoryConfig->getOptions();
63
64
        $metadataDriverName = $metadataFactoryOptions->getMetadataDriver();
65
66
        /** @var DriverInterface $metadataDriver */
67
        $metadataDriver = $serviceLocator->get($metadataDriverName);
68
69
        $hierarchyMetadataClass = $metadataFactoryOptions->getHierarchyMetadataClass();
70
        $debug = $metadataFactoryOptions->getDebug();
71
72
        $r = new ReflectionClass($factoryClassName);
73
        return $r->newInstance(
74
            $metadataDriver,
75
            $hierarchyMetadataClass,
76
            $debug
77
        );
78
    }
79
}
80