Passed
Push — main ( 8323f1...dd18c2 )
by Thierry
02:18
created

Container   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 40
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getService() 0 8 4
A init() 0 4 1
1
<?php
2
3
namespace Lagdo\Symfony\Facades;
4
5
use Symfony\Component\DependencyInjection\ContainerInterface;
6
use Symfony\Component\DependencyInjection\ServiceLocator;
7
8
final class Container
9
{
10
    /**
11
     * @var ContainerInterface
12
     */
13
    protected static $container = null;
14
15
    /**
16
     * @var ServiceLocator
17
     */
18
    protected static $locator = null;
19
20
    /**
21
     * Get a service using the container or the locator.
22
     *
23
     * @param string $serviceId
24
     *
25
     * @return mixed|null
26
     */
27
    public static function getService(string $serviceId)
28
    {
29
        $service = self::$container->get($serviceId, ContainerInterface::NULL_ON_INVALID_REFERENCE);
30
        if($service === null && self::$locator !== null && self::$locator->has($serviceId))
31
        {
32
            $service = self::$locator->get($serviceId);
33
        }
34
        return $service;
35
    }
36
37
    /**
38
     * Set the container and locator.
39
     *
40
     * @param ContainerInterface $container
41
     *
42
     * @return void
43
     */
44
    public static function init(ContainerInterface $container)
45
    {
46
        self::$container = $container;
47
        self::$locator = self::getService('lagdo.facades.service_locator');
48
    }
49
}
50