Completed
Push — master ( 6deaf3...9152f4 )
by Westin
01:48
created

HandlerManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 66
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getServiceConfig() 0 4 1
A hasServiceConfig() 0 4 1
B get() 0 23 4
A setFormatterManager() 0 4 1
A getFormatterManager() 0 10 2
A getFormatter() 0 10 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
    public function hasServiceConfig($id): bool
21
    {
22
        return $this->config->hasHandlerConfig($id);
23
    }
24
25 5
    public function get($id)
26
    {
27 5
        if (key_exists($id, $this->services)) {
28 1
            return $this->services[$id];
29
        }
30
31
        /** @var HandlerInterface $handler */
32 5
        $handler = parent::get($id);
33
34 5
        $config = $this->config->getHandlerConfig($id);
35
36 5
        if (!$config) {
37 2
            return $handler;
38
        }
39
40 3
        $formatter= $config->getFormatter();
41
42 3
        if ($formatter) {
43 3
            $handler->setFormatter($this->getFormatter($formatter));
44
        }
45
46 1
        return $handler;
47
    }
48
49 5
    public function setFormatterManager(FormatterManager $manager)
50
    {
51 5
        $this->formatterManager = $manager;
52 5
    }
53
54 4
    public function getFormatterManager() : FormatterManager
55
    {
56 4
        if (!$this->formatterManager) {
57 1
            throw new MissingServiceException(
58 1
                'Unable to get FormatterManager.'
59
            );
60
        }
61
62 3
        return $this->formatterManager;
63
    }
64
65 3
    protected function getFormatter($id)
66
    {
67 3
        if (!$this->getFormatterManager()->has($id)) {
68 1
            throw new UnknownServiceException(
69 1
                'Unable to locate formatter '.$id
70
            );
71
        }
72
73 1
        return $this->getFormatterManager()->get($id);
74
    }
75
}
76