Application::createContainer()   B
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 31
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 31
rs 8.8571
cc 3
eloc 14
nc 5
nop 0
1
<?php
2
3
namespace Openl10n\Cli;
4
5
use Openl10n\Cli\ServiceContainer\Configuration\ConfigurationLoader;
6
use Openl10n\Cli\ServiceContainer\Exception\ConfigurationLoadingException;
7
use Openl10n\Cli\ServiceContainer\ExtensionManager;
8
use Symfony\Component\Console\Application as BaseApplication;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
11
class Application extends BaseApplication
12
{
13
    /**
14
     * @var ConfigurationLoader
15
     */
16
    protected $configurationLoader;
17
18
    /**
19
     * @var ExtensionManager
20
     */
21
    protected $extensionManager;
22
23
    /**
24
     * @var ContainerBuilder
25
     */
26
    protected $container;
27
28
    /**
29
     * @var boolean
30
     */
31
    protected $ignoreMissingConfiguration;
32
33
    /**
34
     * @param string              $name                Application name
35
     * @param string              $version             Application version
36
     * @param ConfigurationLoader $configurationLoader Configuration loader
37
     * @param ExtensionManager    $extensionManager    Extension manager
38
     */
39
    public function __construct($name, $version, ConfigurationLoader $configurationLoader, ExtensionManager $extensionManager)
40
    {
41
        parent::__construct($name, $version);
42
43
        $this->configurationLoader = $configurationLoader;
44
        $this->extensionManager = $extensionManager;
45
46
        $this->ignoreMissingConfiguration = false;
47
    }
48
49
    /**
50
     * @return ContainerInterface The container
51
     */
52
    public function getContainer()
53
    {
54
        if (null === $this->container) {
55
            $this->container = $this->createContainer();
56
        }
57
58
        return $this->container;
59
    }
60
61
    /**
62
     * Force container to be recreated.
63
     */
64
    public function destroyContainer()
65
    {
66
        $this->container = null;
67
    }
68
69
    /**
70
     * Ignore when the configuration file is missing.
71
     *
72
     * Useful for commands which don't need the configurated services
73
     * such as the `init` command.
74
     *
75
     * @param boolean $value
76
     */
77
    public function ignoreMissingConfiguration($value = true)
78
    {
79
        $this->ignoreMissingConfiguration = (bool) $value;
80
    }
81
82
    /**
83
     * Create a new container.
84
     *
85
     * @return ContainerInterface
86
     */
87
    protected function createContainer()
88
    {
89
        $container = new ContainerBuilder();
90
91
        // Default configuration.
92
        $container->set('application', $this);
93
        $container->set('configuration.loader', $this->configurationLoader);
94
        $container->set('extension_manager', $this->extensionManager);
95
96
        // Initialize extensions.
97
        $this->extensionManager->initialize($container);
98
99
        // Load configuration.
100
        try {
101
            $rawConfigs = $this->configurationLoader->loadConfiguration();
102
103
            $this->extensionManager->load($rawConfigs, $container);
0 ignored issues
show
Bug introduced by
It seems like $rawConfigs defined by $this->configurationLoader->loadConfiguration() on line 101 can also be of type string; however, Openl10n\Cli\ServiceCont...xtensionManager::load() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
104
        } catch (ConfigurationLoadingException $e) {
105
            // No configuration file found.
106
            // Continue with restricted services if `ignoreMissingConfiguration`
107
            // has been specified.
108
            if (!$this->ignoreMissingConfiguration) {
109
                throw $e;
110
            }
111
        }
112
113
        // Process compiler pass & compile container
114
        $container->compile();
115
116
        return $container;
117
    }
118
}
119