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

ServiceLocator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 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