Passed
Push — main ( 146da0...02e199 )
by Thierry
09:03
created

Container::getFacadeService()   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 Container
12
     */
13
    private static $instance = null;
14
15
    /**
16
     * @var ContainerInterface
17
     */
18
    protected $container = null;
19
20
    /**
21
     * @param ContainerInterface $container
22
     */
23
    private function __construct(ContainerInterface $container)
24
    {
25
        $this->container = $container;
26
    }
27
28
    /**
29
     * Get a service using the container or the locator.
30
     *
31
     * @param string $serviceId
32
     *
33
     * @return mixed|null
34
     */
35
    private function getService(string $serviceId)
36
    {
37
        if($this->container->has($serviceId))
38
        {
39
            // A public service will be found in the container.
40
            return $this->container->get($serviceId);
41
        }
42
43
        /**
44
         * @var ServiceLocator
45
         */
46
        $locator = $this->container->get('lagdo.facades.service_locator',
47
            ContainerInterface::NULL_ON_INVALID_REFERENCE);
48
        // If not found in the container, then look in the service locator.
49
        if($locator !== null && $locator->has($serviceId))
50
        {
51
            return $locator->get($serviceId);
52
        }
53
54
        return null;
55
    }
56
57
    /**
58
     * @param ContainerInterface $container
59
     */
60
    public static function createInstance(ContainerInterface $container)
61
    {
62
        self::$instance = new Container($container);
63
    }
64
65
    /**
66
     * Get a service using the container or the locator.
67
     *
68
     * @param string $serviceId
69
     *
70
     * @return mixed|null
71
     */
72
    public static function getFacadeService(string $serviceId)
73
    {
74
        return self::$instance->getService($serviceId);
75
    }
76
}
77