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

HandlerManager::setFormatterManager()   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 3
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\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
    const INTERFACE = HandlerInterface::class;
13
14
    /** @var FormatterManager */
15
    protected $formatterManager;
16
17
    public function getServiceConfig($id): ConfigInterface
18
    {
19
        return $this->config->getHandlerConfig($id);
20
    }
21
22
    public function get($id)
23
    {
24
        $interface = self::INTERFACE;
25
26 View Code Duplication
        if (key_exists($id, $this->services)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
            && $this->services[$id] instanceof $interface
28
        ) {
29
            return $this->services[$id];
30
        }
31
32
        $config = $this->config->getHandlerConfig($id);
33
34
        /** @var HandlerInterface $handler */
35
        $handler = parent::get($id);
36
37
        $formatter= $config->getFormatter();
38
39
        if ($formatter) {
40
            $handler->setFormatter($this->getFormatter($formatter));
41
        }
42
43
        return $handler;
44
    }
45
46
    public function setFormatterManager(FormatterManager $manager)
47
    {
48
        $this->formatterManager = $manager;
49
    }
50
51
    public function getFormatterManager() : FormatterManager
52
    {
53
        if (!$this->formatterManager) {
54
            throw new MissingServiceException(
55
                'Unable to get FormatterManager.'
56
            );
57
        }
58
59
        return $this->formatterManager;
60
    }
61
62
    protected function getFormatter($id)
63
    {
64
        if (!$this->formatterManager->has($id)) {
65
            throw new UnknownServiceException(
66
                'Unable to locate formatter '.$id
67
            );
68
        }
69
70
        return $this->formatterManager->get($id);
71
    }
72
}
73