ServiceLocator::getByType()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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