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

ChannelChanger::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace WShafer\PSR11MonoLog;
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\MissingConfigException;
10
use WShafer\PSR11MonoLog\Exception\UnknownServiceException;
11
12
class ChannelChanger implements ContainerInterface
13
{
14
    /** @var LoggerInterface[] */
15
    protected $channels = [];
16
17
    /** @var MainConfig */
18
    protected $config;
19
20
    /** @var ContainerInterface */
21
    protected $handlerManager;
22
23
    /** @var ContainerInterface */
24
    protected $processorManager;
25
26
    public function __construct(
27
        MainConfig $config,
28
        ContainerInterface $handlerManager,
29
        ContainerInterface $processorManager
30
    ) {
31
        $this->config = $config;
32
        $this->handlerManager = $handlerManager;
33
        $this->processorManager = $processorManager;
34
    }
35
36
    public function get($id)
37
    {
38
        if (!empty($this->channels[$id])
39
            && $this->channels[$id] instanceof LoggerInterface
40
        ) {
41
            return $this->channels[$id];
42
        }
43
44
        if (!$this->has($id)) {
45
            throw new MissingConfigException(
46
                'Unable to locate channel '.$id
47
            );
48
        }
49
50
        $config = $this->config->getChannelConfig($id);
51
52
        $channel = new Logger($id);
53
54
        $handlersToUse = $config->getHandlers();
55
56
        foreach ($handlersToUse as $handlerToUse) {
57
            $handler = $this->getHandler($handlerToUse);
58
            $channel->pushHandler($handler);
59
        }
60
61
        $processorsToUse = $config->getProcessors();
62
63
        foreach ($processorsToUse as $processorToUse) {
64
            $processor = $this->getProcessor($processorToUse);
65
            $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...
66
        }
67
68
        $this->channels[$id] = $channel;
69
        return $this->channels[$id];
70
    }
71
72
    public function has($id)
73
    {
74
        return $this->config->hasChannelConfig($id);
75
    }
76
77
    protected function getHandler($id)
78
    {
79
        if (!$this->handlerManager->has($id)) {
80
            throw new UnknownServiceException(
81
                'Unable to locate processor '.$id
82
            );
83
        }
84
85
        return $this->handlerManager->get($id);
86
    }
87
88
    protected function getProcessor($id)
89
    {
90
        if (!$this->processorManager->has($id)) {
91
            throw new UnknownServiceException(
92
                'Unable to locate processor '.$id
93
            );
94
        }
95
96
        return $this->processorManager->get($id);
97
    }
98
}
99