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

ChannelChanger::getHandler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace WShafer\PSR11MonoLog\Service;
4
5
use Monolog\Logger;
6
use Psr\Container\ContainerInterface;
7
use Psr\Log\LoggerInterface;
8
use WShafer\PSR11MonoLog\Config\MainConfig;
9
use WShafer\PSR11MonoLog\Exception\UnknownServiceException;
10
11
class ChannelChanger implements ContainerInterface
12
{
13
    /** @var LoggerInterface[] */
14
    protected $channels = [];
15
16
    /** @var MainConfig */
17
    protected $config;
18
19
    /** @var ContainerInterface */
20
    protected $handlerManager;
21
22
    /** @var ContainerInterface */
23
    protected $processorManager;
24
25
    public function __construct(
26
        MainConfig $config,
27
        ContainerInterface $handlerManager,
28
        ContainerInterface $processorManager
29
    ) {
30
        $this->config = $config;
31
        $this->handlerManager = $handlerManager;
32
        $this->processorManager = $processorManager;
33
    }
34
35
    public function get($id)
36
    {
37
        if (!empty($this->channels[$id])
38
            && $this->channels[$id] instanceof LoggerInterface
39
        ) {
40
            return $this->channels[$id];
41
        }
42
43
        if (!$this->has($id)) {
44
            throw new UnknownServiceException(
45
                'Unable to locate channel '.$id
46
            );
47
        }
48
49
        $config = $this->config->getChannelConfig($id);
50
51
        $channel = new Logger($id);
52
53
        $handlersToUse = $config->getHandlers();
54
55
        foreach ($handlersToUse as $handlerToUse) {
56
            $handler = $this->getHandler($handlerToUse);
57
            $channel->pushHandler($handler);
58
        }
59
60
        $processorsToUse = $config->getProcessors();
61
62
        foreach ($processorsToUse as $processorToUse) {
63
            $processor = $this->getProcessor($processorToUse);
64
            $channel->pushProcessor($processor);
0 ignored issues
show
Documentation introduced by
$processor is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65
        }
66
67
        return $this->channels[$id];
68
    }
69
70
    public function has($id)
71
    {
72
        return $this->config->hasChannelConfig($id);
73
    }
74
75
    protected function getHandler($id)
76
    {
77
        if (!$this->handlerManager->has($id)) {
78
            throw new UnknownServiceException(
79
                'Unable to locate processor '.$id
80
            );
81
        }
82
83
        return $this->handlerManager->get($id);
84
    }
85
86
    protected function getProcessor($id)
87
    {
88
        if (!$this->processorManager->has($id)) {
89
            throw new UnknownServiceException(
90
                'Unable to locate processor '.$id
91
            );
92
        }
93
94
        return $this->processorManager->get($id);
95
    }
96
}
97