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

Container   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 37
rs 10
c 2
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setContainer() 0 3 1
A getFacadeService() 0 15 4
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