Test Failed
Pull Request — master (#12)
by
unknown
02:46
created

WebsocketTarget::export()   F

Complexity

Conditions 22
Paths 142

Size

Total Lines 63
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 22
eloc 51
c 1
b 0
f 1
nc 142
nop 1
dl 0
loc 63
rs 3.8166

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 Exception;
7
use Psr\Log\LogLevel;
8
use Seasx\SeasLogger\ArrayHelper;
9
use Seasx\SeasLogger\HtmlColor;
10
use Swoole\WebSocket\Server;
11
12
/**
13
 * Class WebsocketTarget
14
 * @package Seasx\SeasLogger\Targets
15
 */
16
class WebsocketTarget extends AbstractTarget
17
{
18
    const COLOR_RANDOM = 'random';
19
    const COLOR_LEVEL = 'level';
20
    const COLOR_DEFAULT = 'default';
21
22
    /** @var array */
23
    private $colorTemplate = [
24
        'Magenta',
25
        self::COLOR_LEVEL,
26
        self::COLOR_LEVEL,
27
        'DarkGray',
28
        'DarkGray',
29
        self::COLOR_RANDOM,
30
        self::COLOR_LEVEL,
31
        'DarkGray',
32
        self::COLOR_LEVEL,
33
        self::COLOR_LEVEL
34
    ];
35
    /** @var string */
36
    private $default = 'LightGray';
37
    /** @var callable */
38
    private $getServer;
39
40
    /**
41
     * @param callable $function
42
     */
43
    public function setGetServer(callable $function): void
44
    {
45
        $this->getServer = $function;
46
    }
47
48
    /**
49
     * @param array $messages
50
     * @throws Exception
51
     */
52
    public function export(array $messages): void
53
    {
54
        if (!is_callable($this->getServer)) {
55
            return;
56
        }
57
        /** @var Server $server */
58
        $swooleServer = call_user_func($this->getServer);
59
        if (!$swooleServer || !$swooleServer instanceof Server) {
60
            return;
61
        }
62
        foreach ($swooleServer->connections as $fd) {
63
            if ($swooleServer->isEstablished($fd)) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $swooleServer->isEstablished($fd) targeting Swoole\WebSocket\Server::isEstablished() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
64
                foreach ($messages as $message) {
65
                    foreach ($message as $msg) {
66
                        if (is_string($msg)) {
67
                            switch (ini_get('seaslog.appender')) {
68
                                case '2':
69
                                case '3':
70
                                    $msg = trim(substr($msg, $this->str_n_pos($msg, ' ', 6)));
71
                                    break;
72
                            }
73
                            $msg = explode($this->split, trim($msg));
74
                            $ranColor = $this->default;
75
                        } else {
76
                            $ranColor = ArrayHelper::remove($msg, '%c');
77
                        }
78
                        if (!empty($this->levelList) && !in_array(strtolower($msg[$this->levelIndex]),
79
                                $this->levelList)) {
80
                            continue;
81
                        }
82
                        if (empty($ranColor)) {
83
                            $ranColor = $this->default;
84
                        } elseif (is_array($ranColor) && isset($ranColor['websocket'])) {
85
                            $ranColor = $ranColor['websocket'];
86
                        } else {
87
                            $ranColor = $this->default;
88
                        }
89
                        $colors = [];
90
                        foreach ($msg as $index => $msgValue) {
91
                            $msg[$index] = is_string($msgValue) ? trim($msgValue) : (string)$msgValue;
92
                            $level = $this->getLevelColor(trim($msg[$this->levelIndex]));
93
                            if (isset($this->colorTemplate[$index])) {
94
                                $color = $this->colorTemplate[$index];
95
                                switch ($color) {
96
                                    case self::COLOR_LEVEL:
97
                                        $colors[] = HtmlColor::getColor($level);
98
                                        break;
99
                                    case self::COLOR_RANDOM:
100
                                        $colors[] = HtmlColor::getColor($ranColor);
101
                                        break;
102
                                    case self::COLOR_DEFAULT:
103
                                        $colors[] = $this->default;
104
                                        break;
105
                                    default:
106
                                        $colors[] = HtmlColor::getColor($color);
107
                                }
108
                            } else {
109
                                $colors[] = $level;
110
                            }
111
                        }
112
                        $msg = json_encode([$msg, $colors], JSON_UNESCAPED_UNICODE);
113
                        rgo(function () use ($swooleServer, $fd, $msg) {
114
                            $swooleServer->push($fd, $msg);
115
                        });
116
                    }
117
                }
118
            }
119
        }
120
    }
121
122
    /**
123
     * @param string $level
124
     * @return string
125
     */
126
    private function getLevelColor(string $level): string
127
    {
128
        switch (strtolower($level)) {
129
            case LogLevel::INFO:
130
                return "Green";
131
            case LogLevel::DEBUG:
132
                return 'DarkGray';
133
            case LogLevel::ERROR:
134
                return "Red";
135
            case LogLevel::WARNING:
136
                return 'Yellow';
137
            default:
138
                return 'DarkRed';
139
        }
140
    }
141
142
}