Test Failed
Push — CI ( 785a66...02428e )
by Adam
54:23
created

LoggerManager   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 2
cbo 1
dl 0
loc 111
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __call() 0 6 3
A log() 0 6 3
A wouldLog() 0 6 3
A assert() 0 3 1
A setLevel() 0 5 2
A getLogger() 0 3 1
A setLogger() 0 3 1
A getAvailableLoggers() 0 3 1
A getLoggerLevels() 0 3 1
1
<?php
2
/**
3
 * Created by Adam Jakab.
4
 * Date: 08/03/16
5
 * Time: 12.37
6
 */
7
8
namespace SuiteCrm\Install;
9
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * Custom implementation of include/SugarLogger/LoggerManager.php
14
 *
15
 * Class LoggerManager
16
 * @package SuiteCrm\Install
17
 */
18
class LoggerManager {
19
    /** @var  OutputInterface */
20
    protected $cmdOutput;
21
22
    /** @var bool */
23
    protected $logToConsole = TRUE;
24
25
    /** @var string */
26
    protected $defaultLogLevel = 'debug';
27
28
    /** @var array */
29
    private static $logLevelMapping = [
30
        'debug' => 100,
31
        'info' => 70,
32
        'warn' => 50,
33
        'deprecated' => 40,
34
        'error' => 25,
35
        'fatal' => 10,
36
        'security' => 5,
37
        'off' => 0,
38
    ];
39
40
    /**
41
     * @param OutputInterface $cmdOutput
42
     * @param string          $defaultLogLevel
43
     */
44
    public function __construct(OutputInterface $cmdOutput, $defaultLogLevel = 'debug') {
45
        $this->cmdOutput = $cmdOutput;
46
        $this->setLevel($defaultLogLevel);
47
    }
48
49
    /**
50
     * @param string $logLevel
51
     * @param mixed  $message
52
     */
53
    public function __call($logLevel, $message) {
54
        $logLevel = (!in_array($logLevel, array_keys(self::$logLevelMapping)) ? $this->defaultLogLevel : $logLevel);
55
        if ($this->wouldLog($logLevel)) {
56
            $this->log($message, $logLevel);
57
        }
58
    }
59
60
    /**
61
     * @param string $msg
62
     * @param string $level
63
     */
64
    protected function log($msg, $level) {
65
        if ($this->logToConsole) {
66
            $msg = is_array($msg) ? implode(" - ", $msg) : $msg;
67
            $this->cmdOutput->writeln("[$level]: " . $msg);
68
        }
69
    }
70
71
    /**
72
     * @param string $logLevel
73
     * @return bool
74
     */
75
    public function wouldLog($logLevel) {
76
        $logLevel = (!in_array($logLevel, array_keys(self::$logLevelMapping)) ? $this->defaultLogLevel : $logLevel);
77
        $wouldLog = $logLevel == $this->defaultLogLevel
78
                    || self::$logLevelMapping[$this->defaultLogLevel] >= self::$logLevelMapping[$logLevel];
79
        return $wouldLog;
80
    }
81
82
    /**
83
     * @param string $message
84
     * @param boolean $condition
85
     */
86
    public function assert($message, $condition) {
87
        //do nothing
88
    }
89
90
    /**
91
     * @param string $logLevel
92
     */
93
    public function setLevel($logLevel) {
94
        if(in_array($logLevel, array_keys(self::$logLevelMapping))) {
95
            $this->defaultLogLevel = $logLevel;
96
        }
97
    }
98
99
    /**
100
     * @throws \Exception
101
     */
102
    public static function getLogger() {
103
        throw new \Exception("getLogger is not available now!");
104
    }
105
106
    /**
107
     * @param string $level
108
     * @param string $logger
109
     */
110
    public static function setLogger($level, $logger) {
111
        //do nothing
112
    }
113
114
    /**
115
     * @return array
116
     */
117
    public static function getAvailableLoggers() {
118
        return [];
119
    }
120
121
    /**
122
     * @return array
123
     */
124
    public static function getLoggerLevels() {
125
        return self::$logLevelMapping;
126
    }
127
128
}