Completed
Push — master ( e9e666...e79deb )
by Roni
01:22
created

LoggerFactory::getContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the XiideaEasyAuditBundle package.
5
 *
6
 * (c) Xiidea <http://www.xiidea.net>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Xiidea\EasyAuditBundle\Logger;
13
14
use Xiidea\EasyAuditBundle\Entity\BaseAuditLog;
15
use Xiidea\EasyAuditBundle\Exception\InvalidServiceException;
16
17
class LoggerFactory
18
{
19
    /** @var LoggerInterface[] */
20
    static private $loggers = array();
21
22
    private $loggersChannel;
23
24
    private $debug = false;
25
26
    public function __construct(array $channel = array())
27
    {
28
        $this->loggersChannel = $channel;
29
    }
30
31
    /**
32
     * @param null|\Xiidea\EasyAuditBundle\Entity\BaseAuditLog $eventInfo
33
     */
34
    public function executeLoggers($eventInfo)
35
    {
36
        if (empty($eventInfo)) {
37
            return;
38
        }
39
40
        foreach (self::$loggers as $id => $logger) {
41
            if ($this->isValidLoggerForThisEvent($eventInfo, $logger, $id)) {
42
                $logger->log($eventInfo);
43
            }
44
        }
45
    }
46
47
    /**
48
     * @param string $loggerName
49
     * @param LoggerInterface $logger
50
     * @throws InvalidServiceException
51
     */
52
    public function addLogger($loggerName, $logger)
53
    {
54
        if ($logger instanceof LoggerInterface) {
55
            self::$loggers[$loggerName] = $logger;
56
        } elseif ($this->debug) {
57
            throw new InvalidServiceException('Logger Service must implement' . __NAMESPACE__ . "LoggerInterface");
58
        }
59
    }
60
61
    /**
62
     * @param string $id
63
     * @param string $level
64
     * @return bool
65
     */
66
    private function isChannelRegisterWithLogger($id, $level)
67
    {
68
        if (!isset($this->loggersChannel[$id])) {
69
            return true;
70
        }
71
72
        if ($this->isChannelTypeOf('inclusive', $id)) {
73
            return $this->levelExistsInList($level, $id);
74
        }
75
76
        if ($this->isChannelTypeOf('exclusive', $id)) {
77
            return !$this->levelExistsInList($level, $id);
78
        }
79
80
        return false;
81
    }
82
83
    /**
84
     * @param string $type
85
     * @param string $id
86
     * @return bool
87
     */
88
    private function isChannelTypeOf($type, $id)
89
    {
90
        return $this->loggersChannel[$id]['type'] == $type;
91
    }
92
93
    /**
94
     * @param string $level
95
     * @param string $id
96
     * @return bool
97
     */
98
    private function levelExistsInList($level, $id)
99
    {
100
        return in_array($level, $this->loggersChannel[$id]['elements']);
101
    }
102
103
    /**
104
     * @param BaseAuditLog $eventInfo
105
     * @param $logger
106
     * @param $id
107
     * @return bool
108
     */
109
    protected function isValidLoggerForThisEvent(BaseAuditLog $eventInfo, $logger, $id)
110
    {
111
        return $logger instanceof LoggerInterface && $this->isChannelRegisterWithLogger($id, $eventInfo->getLevel());
112
    }
113
114
    /**
115
     * @param mixed $debug
116
     */
117
    public function setDebug($debug)
118
    {
119
        $this->debug = $debug;
120
    }
121
}
122