Passed
Branch dev (1a31b5)
by Dispositif
03:03
created

Logger::log()   D

Complexity

Conditions 18
Paths 20

Size

Total Lines 47
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 18
eloc 36
c 1
b 0
f 0
nc 20
nop 3
dl 0
loc 47
rs 4.8666

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
 * This file is part of dispositif/wikibot application
4
 * 2019 © Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the LICENSE file.
6
 */
7
8
declare(strict_types=1);
9
10
11
namespace App\Infrastructure;
12
13
use App\Application\Color;
14
use Psr\Log\LoggerInterface;
15
use Psr\Log\LoggerTrait;
16
17
class Logger implements LoggerInterface
18
{
19
    use LoggerTrait;
20
21
    public $verbose = false;
22
    public $debug = false;
23
24
    /**
25
     * Ultralight logger.
26
     *
27
     * @inheritDoc
28
     */
29
    public function log($level, $message, array $context = [])
30
    {
31
        if (is_array($message)) {
32
            dump($message);
33
            echo "Envoi de array comme $message de log...\n";
34
35
            return;
36
        }
37
        switch ($level) {
38
            case 'emergency':
39
            case 'alert':
40
            case 'critical':
41
                echo Color::BG_RED.Color::WHITE.">>> ".$message."\n".Color::NORMAL;
42
                if (!empty($context)) {
43
                    dump($context);
44
                }
45
                $this->logInFile($level, $message);
46
                break;
47
            case 'error':
48
            case 'warning':
49
                echo Color::BG_YELLOW.Color::BLACK.">>> ".$message."\n".Color::NORMAL;
50
                if (!empty($context)) {
51
                    dump($context);
52
                }
53
                break;
54
            case 'notice':
55
                echo ">>> ".$message."\n".Color::NORMAL;
56
                if (!empty($context)) {
57
                    dump($context);
58
                }
59
                break;
60
            case 'info':
61
                if ($this->verbose || $this->debug) {
62
                    echo Color::GRAY."> ";
63
                    if (!empty($context)) {
64
                        dump($context);
65
                    }
66
                }
67
                break;
68
            case 'debug':
69
                if ($this->debug) {
70
                    echo Color::GRAY."> ";
71
                    if (!empty($context)) {
72
                        dump($context);
73
                    }
74
                }
75
                break;
76
        }
77
    }
78
79
    private function logInFile($level, string $message)
80
    {
81
        file_put_contents(__DIR__.'/resources/critical.log', date('d-m-Y H:i')." : $level : ".$message, FILE_APPEND);
82
    }
83
84
}
85