ServiceManagerFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 48
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setApplicationConfig() 0 4 1
A getApplicationConfig() 0 4 1
A getServiceManager() 0 18 3
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace ZfrCorsTest\Util;
21
22
use Zend\ServiceManager\ServiceManager;
23
use Zend\Mvc\Service\ServiceManagerConfig;
24
25
/**
26
 * Base test case to be used when a new service manager instance is required
27
 *
28
 * @license MIT
29
 * @link    https://github.com/zf-fr/zfr-cors
30
 * @author  Marco Pivetta <[email protected]>
31
 * @author  Florent Blaison <[email protected]>
32
 */
33
abstract class ServiceManagerFactory
34
{
35
    /**
36
     * @var array
37
     */
38
    private static $config = [];
39
40
    /**
41
     * @static
42
     * @param array $config
43
     */
44
    public static function setApplicationConfig(array $config)
45
    {
46
        static::$config = $config;
0 ignored issues
show
Bug introduced by
Since $config is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $config to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
47
    }
48
49
    /**
50
     * @static
51
     * @return array
52
     */
53
    public static function getApplicationConfig()
54
    {
55
        return static::$config;
0 ignored issues
show
Bug introduced by
Since $config is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $config to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
56
    }
57
58
    /**
59
     * @param  array|null     $config
60
     * @return ServiceManager
61
     */
62
    public static function getServiceManager(array $config = null)
63
    {
64
        $config = $config ?: static::getApplicationConfig();
65
        $serviceManager = new ServiceManager();
66
        $serviceManagerConfig = new ServiceManagerConfig(
67
            isset($config['service_manager']) ? $config['service_manager'] : []
68
        );
69
        $serviceManagerConfig->configureServiceManager($serviceManager);
70
71
        $serviceManager->setService('ApplicationConfig', $config);
72
73
        /* @var $moduleManager \Zend\ModuleManager\ModuleManagerInterface */
74
        $moduleManager = $serviceManager->get('ModuleManager');
75
76
        $moduleManager->loadModules();
77
78
        return $serviceManager;
79
    }
80
}
81