ServiceContainer::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @file
5
 * Creates or returns a container to manage global services.
6
 */
7
8
namespace Itafroma\Zork;
9
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\Config\FileLocator;
12
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
13
14
class ServiceContainer
15
{
16
    const DEFAULT_CONFIG = __DIR__ . '/../config/';
17
18
    /**
19
     * Retrieves the service container, ensuring only one instance exists.
20
     *
21
     * @param boolean $reset Creates a new service container, destroying the old one.
22
     * @param string $config_file The path to the YAML configuration file defining the service container.
23
     * @return Symfony\Component\DependencyInjection\ContainerBuilder The service container.
24
     */
25 2
    public static function getContainer($reset = false, $config_file = self::DEFAULT_CONFIG)
26
    {
27 2
        static $container = null;
28
29 2
        if ($container === null || $reset) {
30 2
            $container = static::create($config_file);
31 2
        }
32
33 2
        return $container;
34
    }
35
36
    /**
37
     * Creates a new service container.
38
     *
39
     * @param string $config_file The path to the YAML configuration file defining the service container.
40
     * @return Symfony\Component\DependencyInjection\ContainerBuilder The service container.
41
     */
42 1
    public static function create($config_file = self::DEFAULT_CONFIG)
43
    {
44 1
        $container = new ContainerBuilder();
45 1
        $loader = new YamlFileLoader($container, new FileLocator($config_file));
46 1
        $loader->load('services.yml');
47
48 1
        return $container;
49
    }
50
}
51