ServiceManagerFactory::getApplicationConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace JhFlexiTimeTest\Util;
4
5
use Zend\Mvc\Service\ServiceManagerConfig;
6
use Zend\ServiceManager\ServiceManager;
7
use Zend\ServiceManager\ServiceLocatorInterface;
8
9
use Doctrine\Common\DataFixtures\Purger\ORMPurger as FixturePurger;
10
use Doctrine\Common\DataFixtures\Executor\ORMExecutor as FixtureExecutor;
11
12
use Doctrine\ORM\Tools\SchemaTool;
13
use Doctrine\DBAL\Types\Type;
14
15
/**
16
 * Base test case to be used when a new service manager instance is required
17
 *
18
 * @license MIT
19
 * @link    https://github.com/zf-fr/ZfrRest
20
 * @author  Marco Pivetta <[email protected]>
21
 */
22
abstract class ServiceManagerFactory
23
{
24
    /**
25
     * @var array
26
     */
27
    private static $config = [];
28
29
    /**
30
     * @static
31
     * @param array $config
32
     */
33
    public static function setApplicationConfig(array $config)
34
    {
35
        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...
36
    }
37
38
    /**
39
     * @static
40
     * @return array
41
     */
42
    public static function getApplicationConfig()
43
    {
44
        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...
45
    }
46
47
    /**
48
     * @param array|null $config
49
     * @return ServiceManager
50
     */
51
    public static function getServiceManager(array $config = null)
52
    {
53
        $config = $config ?: static::getApplicationConfig();
54
55
        $serviceManager = new ServiceManager(
56
            new ServiceManagerConfig(
57
                isset($config['service_manager']) ? $config['service_manager'] : []
58
            )
59
        );
60
        $serviceManager->setService('ApplicationConfig', $config);
61
62
        /* @var $moduleManager \Zend\ModuleManager\ModuleManagerInterface */
63
        $moduleManager = $serviceManager->get('ModuleManager');
64
65
        $moduleManager->loadModules();
66
67
        // @todo move to own factory class/add to merged configuration? Create a test module?
68
        $serviceManager->setFactory(
69
            'Doctrine\Common\DataFixtures\Executor\AbstractExecutor',
70
            function (ServiceLocatorInterface $sl) {
71
                /* @var $em \Doctrine\ORM\EntityManager */
72
                $em = $sl->get('Doctrine\ORM\EntityManager');
73
                $schemaTool = new SchemaTool($em);
74
75
                Type::overrideType('date', 'JhFlexiTime\DBAL\Types\DateType');
76
                Type::overrideType('time', 'JhFlexiTime\DBAL\Types\TimeType');
77
78
                $schemaTool->dropDatabase();
79
                $schemaTool->createSchema($em->getMetadataFactory()->getAllMetadata());
80
                return new FixtureExecutor($em, new FixturePurger($em));
81
            }
82
        );
83
84
        return $serviceManager;
85
    }
86
}
87