Test Failed
Pull Request — master (#12)
by wujunze
03:09
created

StyleTarget::export()   F

Complexity

Conditions 18
Paths 246

Size

Total Lines 51
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 18
eloc 42
c 1
b 0
f 1
nc 246
nop 1
dl 0
loc 51
rs 3.4583

How to fix   Long Method    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
declare(strict_types=1);
3
4
namespace Seasx\SeasLogger\Targets;
5
6
use Psr\Log\LogLevel;
7
use Seasx\SeasLogger\ArrayHelper;
8
use Seasx\SeasLogger\ConsoleColor;
9
10
/**
11
 * Class StyleTarget
12
 * @package Seasx\SeasLogger\Targets
13
 */
14
class StyleTarget extends AbstractTarget
15
{
16
    const COLOR_RANDOM = 'random';
17
    const COLOR_DEFAULT = 'default';
18
    const COLOR_LEVEL = 'level';
19
    /** @var ConsoleColor */
20
    private $color;
21
    /** @var array */
22
    private $colorTemplate = [
23
        'magenta',
24
        self::COLOR_LEVEL,
25
        self::COLOR_LEVEL,
26
        'dark_gray',
27
        'dark_gray',
28
        self::COLOR_RANDOM,
29
        self::COLOR_LEVEL,
30
        'dark_gray',
31
        self::COLOR_LEVEL
32
    ];
33
    private $default = 'none';
34
    /** @var string */
35
    private $splitColor = 'cyan';
36
37
    /**
38
     * StyleTarget constructor.
39
     * @param array $levelList
40
     * @param ConsoleColor|null $color
41
     */
42
    public function __construct(array $levelList = [], ?ConsoleColor $color = null)
43
    {
44
        if ($color === null) {
45
            $color = new ConsoleColor();
46
        }
47
        $this->color = $color;
48
        $this->levelList = $levelList;
49
    }
50
51
    /**
52
     * @param array $messages
53
     */
54
    public function export(array $messages): void
55
    {
56
        foreach ($messages as $message) {
57
            foreach ($message as $msg) {
58
                if (is_string($msg)) {
59
                    switch (ini_get('seaslog.appender')) {
60
                        case '2':
61
                        case '3':
62
                            $msg = trim(substr($msg, $this->str_n_pos($msg, ' ', 6)));
63
                            break;
64
                    }
65
                    $msg = explode($this->split, trim($msg));
66
                    $ranColor = $this->default;
67
                } else {
68
                    $ranColor = ArrayHelper::remove($msg, '%c');
69
                }
70
                if (!empty($this->levelList) && !in_array(strtolower($msg[$this->levelIndex]), $this->levelList)) {
71
                    continue;
72
                }
73
                if (empty($ranColor)) {
74
                    $ranColor = $this->default;
75
                } elseif (is_array($ranColor) && isset($ranColor['console'])) {
76
                    $ranColor = $ranColor['console'];
77
                } else {
78
                    $ranColor = $this->default;
79
                }
80
                $context = [];
81
                foreach ($msg as $index => $msgValue) {
82
                    $level = $this->getLevelColor(trim($msg[$this->levelIndex]));
83
                    if (isset($this->colorTemplate[$index])) {
84
                        $color = $this->colorTemplate[$index];
85
                        $msgValue = is_string($msgValue) ? trim($msgValue) : (string)$msgValue;
86
                        switch ($color) {
87
                            case self::COLOR_LEVEL:
88
                                $context[] = $this->color->apply($level, $msgValue);
89
                                break;
90
                            case self::COLOR_DEFAULT:
91
                                $context[] = $this->color->apply($this->default, $msgValue);
92
                                break;
93
                            case self::COLOR_RANDOM:
94
                                $context[] = $this->color->apply($ranColor, $msgValue);
95
                                break;
96
                            default:
97
                                $context[] = $this->color->apply($color, $msgValue);
98
                        }
99
                    } else {
100
                        $context[] = $this->color->apply($level, $msgValue);
101
                    }
102
                }
103
                if (isset($context)) {
104
                    echo implode(' ' . $this->color->apply($this->splitColor, '|') . ' ', $context) . PHP_EOL;
105
                }
106
            }
107
        }
108
    }
109
110
    /**
111
     * @param string $level
112
     * @return string
113
     */
114
    private function getLevelColor(string $level): string
115
    {
116
        switch (strtolower($level)) {
117
            case LogLevel::INFO:
118
                return "green";
119
            case LogLevel::DEBUG:
120
                return 'dark_gray';
121
            case LogLevel::ERROR:
122
                return "red";
123
            case LogLevel::WARNING:
124
                return 'yellow';
125
            default:
126
                return 'light_red';
127
        }
128
    }
129
130
}