Completed
Push — master ( acaf00...ffceb6 )
by Tomáš
03:10
created

ServiceLocator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 42
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setServiceByTypeMap() 0 4 1
A getByType() 0 11 2
A hasByType() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\ActionAutowire\DependencyInjection;
11
12
use Symfony\Component\DependencyInjection\Container;
13
14
final class ServiceLocator
15
{
16
    /**
17
     * @var Container
18
     */
19
    private $container;
20
21
    /**
22
     * @var string[]
23
     */
24
    private $serviceByTypeMap;
25
26 3
    public function __construct(Container $container)
27
    {
28 3
        $this->container = $container;
29 3
    }
30
31 2
    public function setServiceByTypeMap(array $serviceByTypeMap)
32
    {
33 2
        $this->serviceByTypeMap = $serviceByTypeMap;
34 2
    }
35
36
    /**
37
     * @return object|false
38
     */
39 2
    public function getByType(string $type)
40
    {
41 2
        if (! isset($this->serviceByTypeMap[$type])) {
42 1
            return false;
43
        }
44
45 2
        $serviceIds = $this->serviceByTypeMap[$type];
46 2
        $serviceId = reset($serviceIds);
47
48 2
        return $this->container->get($serviceId);
49
    }
50
51 1
    public function hasByType(string $type) : bool
52
    {
53 1
        return isset($this->serviceByTypeMap[$type]);
54
    }
55
}
56