Test Failed
Push — master ( a29797...6ce97f )
by huang
06:37
created

StashFormatter::formatV1()   D

Complexity

Conditions 11
Paths 128

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 11.8766

Importance

Changes 0
Metric Value
cc 11
eloc 22
nc 128
nop 1
dl 0
loc 36
rs 4.9629
c 0
b 0
f 0
ccs 25
cts 31
cp 0.8065
crap 11.8766

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
 * @author    jan huang <[email protected]>
4
 * @copyright 2017
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      https://fastdlabs.com
8
 */
9
10
namespace FastD\Logger\Formatter;
11
12
use Monolog\Formatter\LogstashFormatter;
13
14
/**
15
 * Class StashFormatter.
16
 */
17
class StashFormatter extends LogstashFormatter
18
{
19 37
    public function __construct()
20
    {
21 37
        parent::__construct(app()->getName(), get_local_ip(), null, null, self::V1);
22 37
    }
23
24 16
    public function formatV1(array $record)
25
    {
26 16
        if (empty($record['datetime'])) {
27
            $record['datetime'] = gmdate('c');
28
        }
29
        $message = array(
30 16
            '@timestamp' => $record['datetime'],
31 16
            '@version' => version(),
32 16
            'host' => $this->systemName,
33 16
        );
34 16
        if (isset($record['message'])) {
35 16
            $message['message'] = $record['message'];
36 16
        }
37 16
        if (isset($record['channel'])) {
38 16
            $message['channel'] = $record['channel'];
39 16
        }
40 16
        if (isset($record['level_name'])) {
41 16
            $message['level'] = $record['level_name'];
42 16
        }
43 16
        if (!empty($record['extra'])) {
44
            foreach ($record['extra'] as $key => $val) {
45
                $message[$this->extraPrefix.$key] = $val;
46
            }
47
        }
48 16
        if (!empty($record['context'])) {
49 15
            foreach ($record['context'] as $key => $val) {
50 15
                $message[$this->contextPrefix.$key] = $val;
51 15
            }
52 15
        }
53
54 16
        if (isset($message['trace']) && is_array($message['trace'])) {
55 12
            $message['trace'] = implode(PHP_EOL, $message['trace']);
56 12
        }
57
58 16
        return $message;
59
    }
60
}
61