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

Container::getService()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
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