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) |
|
|
|
|
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
|
|
|
|
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.