xiidea /
EasyAuditBundle
| 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\Model\BaseAuditLog; |
||
| 15 | use Xiidea\EasyAuditBundle\Exception\InvalidServiceException; |
||
| 16 | |||
| 17 | class LoggerFactory |
||
| 18 | { |
||
| 19 | /** @var LoggerInterface[] */ |
||
| 20 | private static $loggers = array(); |
||
| 21 | |||
| 22 | private $debug = false; |
||
| 23 | |||
| 24 | public function __construct(private array $loggersChannel = array()) |
||
| 25 | { |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param null|\Xiidea\EasyAuditBundle\Model\BaseAuditLog $eventInfo |
||
| 30 | */ |
||
| 31 | public function executeLoggers($eventInfo) |
||
| 32 | { |
||
| 33 | if (empty($eventInfo)) { |
||
| 34 | return; |
||
| 35 | } |
||
| 36 | |||
| 37 | foreach (self::$loggers as $id => $logger) { |
||
| 38 | if ($this->isValidLoggerForThisEvent($eventInfo, $logger, $id)) { |
||
| 39 | $logger->log($eventInfo); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param string $loggerName |
||
| 46 | * @param LoggerInterface $logger |
||
| 47 | * |
||
| 48 | * @throws InvalidServiceException |
||
| 49 | */ |
||
| 50 | public function addLogger($loggerName, $logger) |
||
| 51 | { |
||
| 52 | if ($logger instanceof LoggerInterface) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 53 | self::$loggers[$loggerName] = $logger; |
||
| 54 | } elseif ($this->debug) { |
||
| 55 | throw new InvalidServiceException('Logger Service must implement'.__NAMESPACE__.'LoggerInterface'); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param string $id |
||
| 61 | * @param string $level |
||
| 62 | * |
||
| 63 | * @return bool |
||
| 64 | */ |
||
| 65 | private function isChannelRegisterWithLogger($id, $level) |
||
| 66 | { |
||
| 67 | if (!isset($this->loggersChannel[$id])) { |
||
| 68 | return true; |
||
| 69 | } |
||
| 70 | |||
| 71 | if ($this->isChannelTypeOf('inclusive', $id)) { |
||
| 72 | return $this->levelExistsInList($level, $id); |
||
| 73 | } |
||
| 74 | |||
| 75 | if ($this->isChannelTypeOf('exclusive', $id)) { |
||
| 76 | return !$this->levelExistsInList($level, $id); |
||
| 77 | } |
||
| 78 | |||
| 79 | return false; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param string $type |
||
| 84 | * @param string $id |
||
| 85 | * |
||
| 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 | * |
||
| 97 | * @return bool |
||
| 98 | */ |
||
| 99 | private function levelExistsInList($level, $id) |
||
| 100 | { |
||
| 101 | return in_array($level, $this->loggersChannel[$id]['elements']); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param BaseAuditLog $eventInfo |
||
| 106 | * @param $logger |
||
| 107 | * @param $id |
||
| 108 | * |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | protected function isValidLoggerForThisEvent(BaseAuditLog $eventInfo, $logger, $id) |
||
| 112 | { |
||
| 113 | return $logger instanceof LoggerInterface && $this->isChannelRegisterWithLogger($id, $eventInfo->getLevel()); |
||
| 114 | } |
||
| 115 | |||
| 116 | public function setDebug(mixed $debug) |
||
| 117 | { |
||
| 118 | $this->debug = $debug; |
||
| 119 | } |
||
| 120 | } |
||
| 121 |