Logger::add()   C
last analyzed

Complexity

Conditions 13
Paths 10

Size

Total Lines 48
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 48
ccs 0
cts 26
cp 0
rs 6.6166
c 3
b 0
f 0
cc 13
nc 10
nop 3
crap 182

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace LE_ACME2\Utilities;
4
5
use LE_ACME2\SingletonTrait;
6
7
class Logger {
8
9
    use SingletonTrait;
10
11
    const LEVEL_DISABLED = 0;
12
    const LEVEL_INFO = 1;
13
    const LEVEL_DEBUG = 2;
14
15
    private function __construct() {}
16
17
    protected $_desiredLevel = self::LEVEL_DISABLED;
18
19
    public function setDesiredLevel(int $desiredLevel) {
20
        $this->_desiredLevel = $desiredLevel;
21
    }
22
23
    private \Psr\Log\LoggerInterface|null $_psrLogger = null;
24
25
    public function setPSRLogger(\Psr\Log\LoggerInterface|null $psrLogger) : void {
26
        $this->_psrLogger = $psrLogger;
27
    }
28
29
    public function add(int $level, string $message, array $data = array()) : void {
30
31
        if($level > $this->_desiredLevel)
32
            return;
33
34
        if($this->_psrLogger) {
35
36
            if($level == self::LEVEL_INFO) {
37
                $this->_psrLogger->info($message, $data);
38
                return;
39
            }
40
            if($level == self::LEVEL_DEBUG) {
41
                $this->_psrLogger->debug($message, $data);
42
                return;
43
            }
44
            throw new \RuntimeException('Missing PSR Logger support for level: ' . $level);
45
        }
46
47
        $e = new \Exception();
48
        $trace = $e->getTrace();
49
        unset($trace[0]);
50
51
        $output = '<b>' . date('d-m-Y H:i:s') . ': ' . $message . '</b><br>' . "\n";
52
53
        if($this->_desiredLevel == self::LEVEL_DEBUG) {
54
55
            $step = 0;
56
            foreach ($trace as $traceItem) {
57
58
                if(!isset($traceItem['class']) || !isset($traceItem['function'])) {
59
                    continue;
60
                }
61
62
                $output .= 'Trace #' . $step . ': ' . $traceItem['class'] . '::' . $traceItem['function'] . '<br/>' . "\n";
63
                $step++;
64
            }
65
66
            if ((is_array($data) && count($data) > 0) || !is_array($data))
67
                $output .= "\n" .'<br/>Data:<br/>' . "\n" . '<pre>' . var_export($data, true) . '</pre>';
68
69
            $output .= '<br><br>' . "\n\n";
70
        }
71
72
        if(PHP_SAPI == 'cli') {
73
74
            $output = strip_tags($output);
75
        }
76
        echo $output;
77
    }
78
}