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

KafkaTarget::export()   C

Complexity

Conditions 15
Paths 47

Size

Total Lines 50
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 15
eloc 39
c 1
b 0
f 1
nc 47
nop 1
dl 0
loc 50
rs 5.9166

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
declare(strict_types=1);
3
4
namespace Seasx\SeasLogger\Targets;
5
6
7
use Exception;
8
use Seasx\SeasLogger\AbstractConfig;
9
use Seasx\SeasLogger\ArrayHelper;
10
use Seasx\SeasLogger\Kafka\Producter;
11
12
/**
13
 * Class KafkaTarget
14
 * @package Seasx\SeasLogger\Targets
15
 */
16
class KafkaTarget extends AbstractTarget
17
{
18
    /** @var Producter */
19
    private $client;
20
    /** @var string */
21
    private $topic;
22
    /** @var array */
23
    private $customerTmp = [];
24
    /** @var array */
25
    private $fieldTemplate = [];
26
27
    /**
28
     * KafkaTarget constructor.
29
     * @param Producter $client
30
     * @param array $levelList
31
     * @param string $topic
32
     * @param array $customerTmp
33
     * @param array $fieldTemplate
34
     */
35
    public function __construct(
36
        Producter $client,
37
        array $levelList = [],
38
        string $topic = 'seaslog',
39
        array $customerTmp = [],
40
        array $fieldTemplate = [
41
            ['datetime', 'timespan'],
42
            ['level', 'string'],
43
            ['request_uri', 'string'],
44
            ['request_method', 'string'],
45
            ['clientip', 'string'],
46
            ['requestid', 'string'],
47
            ['filename', 'string'],
48
            ['memoryusage', 'int'],
49
            ['message', 'string']
50
        ]
51
    ) {
52
        $this->client = $client;
53
        $this->topic = $topic;
54
        $this->fieldTemplate = $fieldTemplate;
55
        $this->customerTmp = $customerTmp;
56
        $this->levelList = $levelList;
57
    }
58
59
    /**
60
     * @param array $messages
61
     * @throws Exception
62
     */
63
    public function export(array $messages): void
64
    {
65
        foreach ($messages as $module => $message) {
66
            foreach ($message as $msg) {
67
                if (is_string($msg)) {
68
                    switch (ini_get('seaslog.appender')) {
69
                        case '2':
70
                        case '3':
71
                            $msg = trim(substr($msg, $this->str_n_pos($msg, ' ', 6)));
72
                            break;
73
                        case '1':
74
                        default:
75
                            $fileName = basename($module);
76
                            $module = substr($fileName, 0, strrpos($fileName, '_'));
77
                    }
78
                    $msg = explode($this->split, trim($msg));
79
                } else {
80
                    ArrayHelper::remove($msg, '%c');
81
                }
82
                if (!empty($this->levelList) && !in_array(strtolower($msg[$this->levelIndex]), $this->levelList)) {
83
                    continue;
84
                }
85
                $log = [
86
                    'appname' => $module,
87
                ];
88
                $i = 0;
89
                foreach ($msg as $index => $msgValue) {
90
                    if ($this->template[$index] === '%A') {
91
                        switch ($this->customerType) {
92
                            case AbstractConfig::TYPE_JSON:
93
                                $msgValue = json_decode($msgValue, true);
94
                                break;
95
                            case AbstractConfig::TYPE_FIELD:
96
                            default:
97
                                $msgValue = explode($this->split, $msgValue);
98
                        }
99
                        foreach ($this->customerTmp as $tmpIndex => [$name, $type]) {
100
                            $this->makeLog($log, $name, $type, isset($msgValue[$tmpIndex]) ? $msgValue[$tmpIndex] : '');
101
                        }
102
                    } else {
103
                        [$name, $type] = $this->fieldTemplate[$i];
104
                        $this->makeLog($log, $name, $type, $msgValue);
105
                        $i++;
106
                    }
107
                }
108
                $this->client->send([
109
                    [
110
                        'topic' => $this->topic,
111
                        'value' => json_encode($log),
112
                        'key' => ''
113
                    ]
114
                ]);
115
            }
116
        }
117
    }
118
119
    /**
120
     * @param array $log
121
     * @param string $name
122
     * @param string $type
123
     * @param $value
124
     */
125
    private function makeLog(array &$log, string $name, string $type, $value): void
126
    {
127
        switch ($type) {
128
            case "timespan":
129
                $log[$name] = $value ? strtotime(explode('.', $value)[0]) : 0;
130
                break;
131
            case "int":
132
                $log[$name] = $value ? (int)$value : 0;
133
                break;
134
            case "string":
135
                $log[$name] = $value ? trim($value) : '';
136
                break;
137
            default:
138
                $log[$type][$name] = $value;
139
        }
140
    }
141
}