Completed
Push — master ( 18069b...6f42c8 )
by Tomáš
05:04
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 1
    public function __construct(Container $container)
27
    {
28 1
        $this->container = $container;
29 1
    }
30
31
    public function setServiceByTypeMap(array $serviceByTypeMap)
32
    {
33
        $this->serviceByTypeMap = $serviceByTypeMap;
34
    }
35
36
    /**
37
     * @return object|false
38
     */
39
    public function getByType(string $type)
40
    {
41
        if (! isset($this->serviceByTypeMap[$type])) {
42
            return false;
43
        }
44
45
        $serviceIds = $this->serviceByTypeMap[$type];
46
        $serviceId = reset($serviceIds);
47
48
        return $this->container->get($serviceId);
49
    }
50
51
    public function hasByType(string $type) : bool
52
    {
53
        return isset($this->serviceByTypeMap[$type]);
54
    }
55
}
56