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

Container   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 55
rs 10
c 2
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A getService() 0 11 4
A __construct() 0 3 1
A locator() 0 3 1
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