Container::getFacadeService()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 4
c 2
b 0
f 0
nc 8
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
    private static $container = null;
14
15
    /**
16
     * @var ServiceLocator
17
     */
18
    private static $locator = null;
19
20
    /**
21
     * @param ContainerInterface $container
22
     *
23
     * @return void
24
     */
25
    public static function setContainer(ContainerInterface $container)
26
    {
27
        self::$container = $container;
28
        self::$locator = $container->get('lagdo.facades.service_locator',
29
            ContainerInterface::NULL_ON_INVALID_REFERENCE);
30
    }
31
32
    /**
33
     * Get a service using the container or the locator.
34
     *
35
     * @param string $serviceId
36
     *
37
     * @return mixed
38
     */
39
    public static function getFacadeService(string $serviceId)
40
    {
41
        return self::$container->has($serviceId) ?
42
            // A public service will be found in the container.
43
            self::$container->get($serviceId) :
44
            // If not found in the container, then look in the service locator.
45
            (self::$locator !== null && self::$locator->has($serviceId) ?
46
            self::$locator->get($serviceId) : null);
47
    }
48
}
49