ServiceContainer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 37
ccs 11
cts 11
cp 1
rs 10
c 1
b 0
f 0
wmc 4
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getContainer() 0 10 3
A create() 0 8 1
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