Passed
Branch master (fc5382)
by Fabian
03:13
created

Logger::add()   C

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 28
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
    /** @var \Psr\Log\LoggerInterface|null $_psrLogger */
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
    private $_psrLogger = null;
25
26
    /**
27
     * @param \Psr\Log\LoggerInterface|null $psrLogger
28
     */
29
    public function setPSRLogger($psrLogger) {
30
        $this->_psrLogger = $psrLogger;
31
    }
32
33
    /**
34
     * @param int $level
35
     * @param string $message
36
     * @param array $data
37
     */
38
    public function add(int $level, string $message, array $data = array()) {
39
40
        if($level > $this->_desiredLevel)
41
            return;
42
43
        if($this->_psrLogger) {
44
45
            if($level == self::LEVEL_INFO) {
46
                $this->_psrLogger->info($message, $data);
47
                return;
48
            }
49
            if($level == self::LEVEL_DEBUG) {
50
                $this->_psrLogger->debug($message, $data);
51
                return;
52
            }
53
            throw new \RuntimeException('Missing PSR Logger support for level: ' . $level);
54
        }
55
56
        $e = new \Exception();
57
        $trace = $e->getTrace();
58
        unset($trace[0]);
59
60
        $output = '<b>' . date('d-m-Y H:i:s') . ': ' . $message . '</b><br>' . "\n";
61
62
        if($this->_desiredLevel == self::LEVEL_DEBUG) {
63
64
            $step = 0;
65
            foreach ($trace as $traceItem) {
66
67
                if(!isset($traceItem['class']) || !isset($traceItem['function'])) {
68
                    continue;
69
                }
70
71
                $output .= 'Trace #' . $step . ': ' . $traceItem['class'] . '::' . $traceItem['function'] . '<br/>' . "\n";
72
                $step++;
73
            }
74
75
            if ((is_array($data) && count($data) > 0) || !is_array($data))
76
                $output .= "\n" .'<br/>Data:<br/>' . "\n" . '<pre>' . var_export($data, true) . '</pre>';
77
78
            $output .= '<br><br>' . "\n\n";
79
        }
80
81
        if(PHP_SAPI == 'cli') {
82
83
            $output = strip_tags($output);
84
        }
85
        echo $output;
86
    }
87
}