Completed
Push — master ( 03d74f...6e5fe7 )
by Westin
03:43 queued 01:57
created

AbstractServiceManager   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 6
dl 0
loc 92
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
getServiceConfig() 0 1 ?
A __construct() 0 9 1
B get() 0 22 4
A has() 0 12 3
B getInstanceFromFactory() 0 29 5
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 9
    public function __construct(
28
        MainConfig $config,
29
        MapperInterface $mapper,
30
        ContainerInterface $container
31
    ) {
32 9
        $this->config    = $config;
33 9
        $this->mapper    = $mapper;
34 9
        $this->container = $container;
35 9
    }
36
37
    abstract protected function getServiceConfig($id) : ConfigInterface;
38
39 6
    public function get($id)
40
    {
41 6
        if (key_exists($id, $this->services)) {
42 1
            return $this->services[$id];
43
        }
44
45
        // Make sure we have one of these
46 6
        if (!$this->has($id)) {
47 1
            throw new UnknownServiceException(
48 1
                'Unable to locate service '.$id.'.  Please check your configuration.'
49
            );
50
        }
51
52
        // Check the main container for this service id.
53 5
        if ($this->container->has($id)) {
54 2
            $this->services[$id] = $this->container->get($id);
55 2
            return $this->services[$id];
56
        }
57
58 3
        $this->services[$id] = $this->getInstanceFromFactory($id);
59 2
        return $this->services[$id];
60
    }
61
62 8
    public function has($id)
63
    {
64 8
        if (key_exists($id, $this->services)) {
65 1
            return true;
66
        }
67
68 8
        if ($this->container->has($id)) {
69 3
            return true;
70
        }
71
72 5
        return $this->config->hasHandlerConfig($id);
73
    }
74
75 3
    protected function getInstanceFromFactory($id)
76
    {
77 3
        $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 3
        $config  = $this->getServiceConfig($id);
80 3
        $type    = $config->getType();
81 3
        $options = $config->getOptions();
82
83 3
        $class = $type;
84
85
        // Check for class and class implements of Monolog Formatter Interface
86 3
        if (!class_exists($class)
87 3
            || !in_array(FactoryInterface::class, class_implements($class))
88
        ) {
89 2
            $class = $this->mapper->map($type);
90
        }
91
92 3
        if (!class_exists($class)
93 3
            || !in_array(FactoryInterface::class, class_implements($class))
94
        ) {
95 1
            throw new InvalidConfigException(
96 1
                $id.'.  Is not a valid factory.  Please check your configuration.'
97
            );
98
        }
99
100
        /** @var FactoryInterface $factory */
101 2
        $factory = new $class;
102 2
        return $factory($options);
103
    }
104
}
105