Completed
Push — master ( be1c1a...b74105 )
by Westin
08:25
created

HandlerManager::has()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace WShafer\PSR11MonoLog\Service;
4
5
use Monolog\Handler\HandlerInterface;
6
use WShafer\PSR11MonoLog\ConfigInterface;
7
use WShafer\PSR11MonoLog\Exception\MissingServiceException;
8
use WShafer\PSR11MonoLog\Exception\UnknownServiceException;
9
10
class HandlerManager extends AbstractServiceManager
11
{
12
    /** @var FormatterManager */
13
    protected $formatterManager;
14
15 1
    public function getServiceConfig($id): ConfigInterface
16
    {
17 1
        return $this->config->getHandlerConfig($id);
18
    }
19
20 5
    public function get($id)
21
    {
22 5
        if (key_exists($id, $this->services)) {
23 1
            return $this->services[$id];
24
        }
25
26
        /** @var HandlerInterface $handler */
27 5
        $handler = parent::get($id);
28
29 5
        $config = $this->config->getHandlerConfig($id);
30
31 5
        if (!$config) {
32 2
            return $handler;
33
        }
34
35 3
        $formatter= $config->getFormatter();
36
37 3
        if ($formatter) {
38 3
            $handler->setFormatter($this->getFormatter($formatter));
39
        }
40
41 1
        return $handler;
42
    }
43
44 5
    public function has($id)
45
    {
46 5
        if (parent::has($id)) {
47 5
            return true;
48
        }
49 4
50
        return $this->config->hasHandlerConfig($id);
51 4
    }
52 1
53 1
    public function setFormatterManager(FormatterManager $manager)
54
    {
55
        $this->formatterManager = $manager;
56
    }
57 3
58
    public function getFormatterManager() : FormatterManager
59
    {
60 3
        if (!$this->formatterManager) {
61
            throw new MissingServiceException(
62 3
                'Unable to get FormatterManager.'
63 1
            );
64 1
        }
65
66
        return $this->formatterManager;
67
    }
68 1
69
    protected function getFormatter($id)
70
    {
71
        if (!$this->getFormatterManager()->has($id)) {
72
            throw new UnknownServiceException(
73
                'Unable to locate formatter '.$id
74
            );
75
        }
76
77
        return $this->getFormatterManager()->get($id);
78
    }
79
}
80