Completed
Push — master ( 6b1959...c94e47 )
by Gianluca
9s
created

ServiceManagerFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfiguration() 0 9 2
A getServiceManager() 0 16 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 DoctrineORMModuleTest;
21
22
use Zend\Mvc\Application;
23
use Zend\Mvc\Service\ServiceManagerConfig;
24
use Zend\ServiceManager\ServiceManager;
25
26
/**
27
 * Utility used to retrieve a freshly bootstrapped application's service manager
28
 *
29
 * @license MIT
30
 * @link    http://www.doctrine-project.org/
31
 * @author  Marco Pivetta <[email protected]>
32
 */
33
class ServiceManagerFactory
34
{
35
    /**
36
     * @return array
37
     */
38
    public static function getConfiguration()
39
    {
40
        $r = new \ReflectionClass(Application::class);
41
        $requiredParams = $r->getConstructor()->getNumberOfRequiredParameters();
42
43
        $configFile = $requiredParams == 1 ? 'TestConfigurationV3.php' : 'TestConfigurationV2.php';
44
45
        return include __DIR__ . '/../' . $configFile;
46
    }
47
48
    /**
49
     * Builds a new ServiceManager instance
50
     *
51
     * @param  array|null     $configuration
52
     * @return ServiceManager
53
     */
54
    public static function getServiceManager(array $configuration = null)
55
    {
56
        $configuration        = $configuration ?: static::getConfiguration();
57
        $serviceManager       = new ServiceManager();
58
        $serviceManagerConfig = new ServiceManagerConfig(
59
            isset($configuration['service_manager']) ? $configuration['service_manager'] : array()
60
        );
61
        $serviceManagerConfig->configureServiceManager($serviceManager);
62
        $serviceManager->setService('ApplicationConfig', $configuration);
63
64
        /** @var $moduleManager \Zend\ModuleManager\ModuleManager */
65
        $moduleManager = $serviceManager->get('ModuleManager');
66
        $moduleManager->loadModules();
67
68
        return $serviceManager;
69
    }
70
}
71