Completed
Push — master ( 6d1d70...5b3b49 )
by Westin
02:51
created

AbstractServiceManager::has()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 5
Ratio 22.73 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 5
loc 22
ccs 0
cts 7
cp 0
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 11
nc 4
nop 1
crap 42
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\UnknownServiceException;
9
use WShafer\PSR11MonoLog\FactoryInterface;
10
use WShafer\PSR11MonoLog\MapperInterface;
11
12
abstract class AbstractServiceManager implements ContainerInterface
13
{
14
    /** @var MainConfig */
15
    protected $config;
16
17
    /** @var MapperInterface */
18
    protected $mapper;
19
20
    /** @var ContainerInterface */
21
    protected $container;
22
23
    /** @var array */
24
    protected $services;
25
    
26
    const INTERFACE = '';
27
28
    public function __construct(
29
        MainConfig $config,
30
        MapperInterface $mapper,
31
        ContainerInterface $container
32
    ) {
33
        $this->config    = $config;
34
        $this->mapper    = $mapper;
35
        $this->container = $container;
36
    }
37
38
    abstract protected function getServiceConfig($id) : ConfigInterface;
39
40
    public function get($id)
41
    {
42
        $interface = self::INTERFACE;
43
        
44 View Code Duplication
        if (key_exists($id, $this->services)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
            && $this->services[$id] instanceof $interface
46
        ) {
47
            return $this->services[$id];
48
        }
49
50
        // Make sure we have one of these
51
        if (!$this->has($id)) {
52
            throw new UnknownServiceException(
53
                'Unable to locate service '.$id.'.  Please check your configuration.'
54
            );
55
        }
56
57
        // Check the main container for this service id.
58
        if ($this->container->has($id)) {
59
            $this->services[$id] = $this->container->get($id);
60
            return $this->services[$id];
61
        }
62
63
        // Check for class and class implements of Monolog Interface
64
        if (class_exists($id)
65
            && in_array($interface, class_implements($id))
66
        ) {
67
            $this->services[$id] = new $id;
68
            return $this->services[$id];
69
        }
70
71
        $this->services[$id] = $this->getInstanceFromFactory($id);
72
        return $this->services[$id];
73
    }
74
75
    public function has($id)
76
    {
77
        $interface = self::INTERFACE;
78
79 View Code Duplication
        if (key_exists($id, $this->services)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
            && $this->services[$id] instanceof $interface
81
        ) {
82
            return true;
83
        }
84
85
        if ($this->container->has($id)) {
86
            return true;
87
        }
88
89
        if (class_exists($id)
90
            && in_array($interface, class_implements($id))
91
        ) {
92
            return true;
93
        }
94
95
        return $this->config->hasHandlerConfig($id);
96
    }
97
98
    protected function getInstanceFromFactory($id)
99
    {
100
        $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...
101
102
        $config  = $this->getServiceConfig($id);
103
        $type    = $config->getType();
104
        $options = $config->getOptions();
105
106
        $class = $type;
107
108
        // Check for class and class implements of Monolog Formatter Interface
109
        if (!class_exists($class)
110
            || !in_array(FactoryInterface::class, class_implements($class))
111
        ) {
112
            $class = $this->mapper->map($type);
113
        }
114
115
        if (!class_exists($class)
116
            || !in_array(FactoryInterface::class, class_implements($class))
117
        ) {
118
            throw new UnknownServiceException(
119
                'Unable to locate service '.$id.'.  Please check your configuration.'
120
            );
121
        }
122
123
        /** @var FactoryInterface $factory */
124
        $factory = new $class;
125
        return $factory($options);
126
    }
127
}
128