Completed
Pull Request — master (#165)
by Paul
03:21
created

ServiceManagerConfig::configureServiceManager()   C

Complexity

Conditions 10
Paths 32

Size

Total Lines 48
Code Lines 25

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 48
rs 5.3454
cc 10
eloc 25
nc 32
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the PPI Framework.
4
 *
5
 * @copyright   Copyright (c) 2012 Paul Dragoonis <[email protected]>
6
 * @license     http://opensource.org/licenses/mit-license.php MIT
7
 *
8
 * @link        http://www.ppi.io
9
 */
10
11
namespace PPI\Framework\ServiceManager\Config;
12
13
use Zend\ServiceManager\ConfigInterface;
14
use Zend\ServiceManager\ServiceLocatorAwareInterface;
15
use Zend\ServiceManager\ServiceManager;
16
use Zend\ServiceManager\ServiceManagerAwareInterface;
17
18
/**
19
 * @author     Vítor Brandão <[email protected]>
20
 */
21
class ServiceManagerConfig implements ConfigInterface
22
{
23
    /**
24
     * Services that can be instantiated without factories.
25
     *
26
     * @var array
27
     */
28
    protected $invokables = array(
29
        'SharedEventManager' => 'Zend\EventManager\SharedEventManager',
30
    );
31
32
    /**
33
     * Service factories.
34
     *
35
     * @var array
36
     */
37
    protected $factories = array(
38
        'Config'                => 'PPI\Framework\ServiceManager\Factory\ConfigFactory',
39
        'EventManager'          => 'PPI\Framework\ServiceManager\Factory\EventManagerFactory',
40
        'ModuleDefaultListener' => 'PPI\Framework\ServiceManager\Factory\ModuleDefaultListenerFactory',
41
        'ModuleManager'         => 'PPI\Framework\ServiceManager\Factory\ModuleManagerFactory',
42
    );
43
44
    /**
45
     * Abstract factories.
46
     *
47
     * @var array
48
     */
49
    protected $abstractFactories = array();
50
51
    /**
52
     * Aliases.
53
     *
54
     * @var array
55
     */
56
    protected $aliases = array(
57
        'Zend\EventManager\EventManagerInterface'       => 'EventManager',
58
        'Zend\ServiceManager\ServiceLocatorInterface'   => 'ServiceManager',
59
        'Zend\ServiceManager\ServiceManager'            => 'ServiceManager',
60
    );
61
62
    /**
63
     * Shared services.
64
     *
65
     * Services are shared by default; this is primarily to indicate services
66
     * that should NOT be shared
67
     *
68
     * @var array
69
     */
70
    protected $shared = array(
71
        'EventManager' => false,
72
    );
73
74
    /**
75
     * Constructor.
76
     *
77
     * Merges internal arrays with those passed via configuration
78
     *
79
     * @param array $configuration
80
     */
81
    public function __construct(array $configuration = array())
82
    {
83
        if (isset($configuration['invokables'])) {
84
            $this->invokables = array_merge($this->invokables, $configuration['invokables']);
85
        }
86
87
        if (isset($configuration['factories'])) {
88
            $this->factories = array_merge($this->factories, $configuration['factories']);
89
        }
90
91
        if (isset($configuration['abstract_factories'])) {
92
            $this->abstractFactories = array_merge($this->abstractFactories, $configuration['abstract_factories']);
93
        }
94
95
        if (isset($configuration['aliases'])) {
96
            $this->aliases = array_merge($this->aliases, $configuration['aliases']);
97
        }
98
99
        if (isset($configuration['shared'])) {
100
            $this->shared = array_merge($this->shared, $configuration['shared']);
101
        }
102
    }
103
104
    /**
105
     * Configure the provided service manager instance with the configuration
106
     * in this class.
107
     *
108
     * In addition to using each of the internal properties to configure the
109
     * service manager, also adds an initializer to inject ServiceManagerAware
110
     * and ServiceLocatorAware classes with the service manager.
111
     *
112
     * @param ServiceManager $serviceManager
113
     */
114
    public function configureServiceManager(ServiceManager $serviceManager)
115
    {
116
        foreach ($this->invokables as $name => $class) {
117
            $serviceManager->setInvokableClass($name, $class);
118
        }
119
120
        foreach ($this->factories as $name => $factoryClass) {
121
            $serviceManager->setFactory($name, $factoryClass);
122
        }
123
124
        foreach ($this->abstractFactories as $factoryClass) {
125
            $serviceManager->addAbstractFactory($factoryClass);
126
        }
127
128
        foreach ($this->aliases as $name => $service) {
129
            $serviceManager->setAlias($name, $service);
130
        }
131
132
        foreach ($this->shared as $name => $value) {
133
            $serviceManager->setShared($name, $value);
134
        }
135
136
        $serviceManager->addInitializer(function ($instance) use ($serviceManager) {
137
            if ($instance instanceof EventManagerAwareInterface) {
0 ignored issues
show
Bug introduced by
The class PPI\Framework\ServiceMan...ntManagerAwareInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
138
                if ($instance->getEventManager() instanceof EventManagerInterface) {
0 ignored issues
show
Bug introduced by
The class PPI\Framework\ServiceMan...g\EventManagerInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
139
                    $instance->getEventManager()->setSharedManager(
140
                        $serviceManager->get('SharedEventManager')
141
                    );
142
                } else {
143
                    $instance->setEventManager($serviceManager->get('EventManager'));
144
                }
145
            }
146
        });
147
148
        $serviceManager->addInitializer(function ($instance) use ($serviceManager) {
149
            if ($instance instanceof ServiceManagerAwareInterface) {
150
                $instance->setServiceManager($serviceManager);
151
            }
152
        });
153
154
        $serviceManager->addInitializer(function ($instance) use ($serviceManager) {
155
            if ($instance instanceof ServiceLocatorAwareInterface) {
156
                $instance->setServiceLocator($serviceManager);
157
            }
158
        });
159
160
        $serviceManager->setService('ServiceManager', $serviceManager);
161
    }
162
}
163