Passed
Push — main ( 6266e4...5fc3be )
by Thierry
08:29
created

Container::__construct()   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
    protected $container;
14
15
    /**
16
     * @param ContainerInterface $container
17
     */
18
    public function __construct(ContainerInterface $container)
19
    {
20
        $this->container = $container;
21
    }
22
23
    /**
24
     * Get a service from the container.
25
     *
26
     * @param string $serviceId
27
     *
28
     * @return mixed|null
29
     */
30
    private function get(string $serviceId)
31
    {
32
        return $this->container->get($serviceId, ContainerInterface::NULL_ON_INVALID_REFERENCE);
33
    }
34
35
    /**
36
     * Get the locator.
37
     *
38
     * @return ServiceLocator
39
     */
40
    public function locator()
41
    {
42
        return $this->get('lagdo.facades.service_locator');
43
    }
44
45
    /**
46
     * Get a service using the container or the locator.
47
     *
48
     * @param string $serviceId
49
     *
50
     * @return mixed|null
51
     */
52
    public function getService(string $serviceId)
53
    {
54
        if(($service = $this->get($serviceId)) !== null)
55
        {
56
            return $service;
57
        }
58
        if(($locator = $this->locator()) !== null && $locator->has($serviceId))
59
        {
60
            return $locator->get($serviceId);
61
        }
62
        return null;
63
    }
64
}
65