StashFormatter::formatV1()   D
last analyzed

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 32
    public function __construct()
20
    {
21 32
        parent::__construct(app()->getName(), get_local_ip(), null, null, self::V1);
22 32
    }
23
24
    /**
25
     * @param array $record
26
     *
27
     * @return array
28
     */
29 10
    public function formatV1(array $record)
30
    {
31 10
        if (empty($record['datetime'])) {
32
            $record['datetime'] = gmdate('c');
33
        }
34
        $message = array(
35 10
            '@timestamp' => $record['datetime'],
36 10
            '@version' => version(),
37 10
            'host' => $this->systemName,
38 10
        );
39 10
        if (isset($record['message'])) {
40 10
            $message['message'] = $record['message'];
41 10
        }
42 10
        if (isset($record['channel'])) {
43 10
            $message['channel'] = $record['channel'];
44 10
        }
45 10
        if (isset($record['level_name'])) {
46 10
            $message['level'] = $record['level_name'];
47 10
        }
48 10
        if (!empty($record['extra'])) {
49
            foreach ($record['extra'] as $key => $val) {
50
                $message[$this->extraPrefix.$key] = $val;
51
            }
52
        }
53 10
        if (!empty($record['context'])) {
54 9
            foreach ($record['context'] as $key => $val) {
55 9
                $message[$this->contextPrefix.$key] = $val;
56 9
            }
57 9
        }
58
59 10
        if (isset($message['trace']) && is_array($message['trace'])) {
60 3
            $message['trace'] = implode(PHP_EOL, $message['trace']);
61 3
        }
62
63 10
        return $message;
64
    }
65
}
66