Completed
Push — master ( 03d74f...6e5fe7 )
by Westin
03:43 queued 01:57
created

HandlerManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getServiceConfig() 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 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 setFormatterManager(FormatterManager $manager)
45
    {
46 5
        $this->formatterManager = $manager;
47 5
    }
48
49 4
    public function getFormatterManager() : FormatterManager
50
    {
51 4
        if (!$this->formatterManager) {
52 1
            throw new MissingServiceException(
53 1
                'Unable to get FormatterManager.'
54
            );
55
        }
56
57 3
        return $this->formatterManager;
58
    }
59
60 3
    protected function getFormatter($id)
61
    {
62 3
        if (!$this->getFormatterManager()->has($id)) {
63 1
            throw new UnknownServiceException(
64 1
                'Unable to locate formatter '.$id
65
            );
66
        }
67
68 1
        return $this->getFormatterManager()->get($id);
69
    }
70
}
71