Completed
Push — master ( 5b3b49...e44d2f )
by Westin
05:38
created

AbstractServiceManager::get()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 7
cp 0
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
crap 20
1
<?php
2
3
namespace WShafer\PSR11MonoLog\Service;
4
5
use Psr\Container\ContainerInterface;
6
use WShafer\PSR11MonoLog\Config\MainConfig;
7
use WShafer\PSR11MonoLog\ConfigInterface;
8
use WShafer\PSR11MonoLog\Exception\InvalidConfigException;
9
use WShafer\PSR11MonoLog\Exception\UnknownServiceException;
10
use WShafer\PSR11MonoLog\FactoryInterface;
11
use WShafer\PSR11MonoLog\MapperInterface;
12
13
abstract class AbstractServiceManager implements ContainerInterface
14
{
15
    /** @var MainConfig */
16
    protected $config;
17
18
    /** @var MapperInterface */
19
    protected $mapper;
20
21
    /** @var ContainerInterface */
22
    protected $container;
23
24
    /** @var array */
25
    protected $services = [];
26
27
    public function __construct(
28
        MainConfig $config,
29
        MapperInterface $mapper,
30
        ContainerInterface $container
31
    ) {
32
        $this->config    = $config;
33
        $this->mapper    = $mapper;
34
        $this->container = $container;
35
    }
36
37
    abstract protected function getServiceConfig($id) : ConfigInterface;
38
39
    public function get($id)
40
    {
41
        if (key_exists($id, $this->services)) {
42
            return $this->services[$id];
43
        }
44
45
        // Make sure we have one of these
46
        if (!$this->has($id)) {
47
            throw new UnknownServiceException(
48
                'Unable to locate service '.$id.'.  Please check your configuration.'
49
            );
50
        }
51
52
        // Check the main container for this service id.
53
        if ($this->container->has($id)) {
54
            $this->services[$id] = $this->container->get($id);
55
            return $this->services[$id];
56
        }
57
58
        $this->services[$id] = $this->getInstanceFromFactory($id);
59
        return $this->services[$id];
60
    }
61
62
    public function has($id)
63
    {
64
        if (key_exists($id, $this->services)) {
65
            return true;
66
        }
67
68
        if ($this->container->has($id)) {
69
            return true;
70
        }
71
72
        return $this->config->hasHandlerConfig($id);
73
    }
74
75
    protected function getInstanceFromFactory($id)
76
    {
77
        $class   = null;
0 ignored issues
show
Unused Code introduced by
$class is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
78
79
        $config  = $this->getServiceConfig($id);
80
        $type    = $config->getType();
81
        $options = $config->getOptions();
82
83
        $class = $type;
84
85
        // Check for class and class implements of Monolog Formatter Interface
86
        if (!class_exists($class)
87
            || !in_array(FactoryInterface::class, class_implements($class))
88
        ) {
89
            $class = $this->mapper->map($type);
90
        }
91
92
        if (!class_exists($class)
93
            || !in_array(FactoryInterface::class, class_implements($class))
94
        ) {
95
            throw new InvalidConfigException(
96
                $id.'.  Is not a valid factory.  Please check your configuration.'
97
            );
98
        }
99
100
        /** @var FactoryInterface $factory */
101
        $factory = new $class;
102
        return $factory($options);
103
    }
104
}
105