Passed
Push — main ( 11ffe7...c92b36 )
by Thierry
02:21
created

Container::setContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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