EloquentFormatter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JustSteveKing\EloquentLogDriver\Formatter;
6
7
use Illuminate\Support\Str;
8
use Monolog\Formatter\NormalizerFormatter;
9
10
class EloquentFormatter extends NormalizerFormatter
11
{
12
    /**
13
     * type
14
     */
15
    const LOG = 'log';
16
    const STORE = 'store';
17
    const CHANGE = 'change';
18
    const DELETE = 'delete';
19
20
    /**
21
     * result
22
     */
23
    const SUCCESS = 'success';
24
    const NEUTRAL = 'neutral';
25
    const FAILURE = 'failure';
26
27
    public function __construct()
28
    {
29
        parent::__construct();
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function format(array $record)
36
    {
37
        $record = parent::format($record);
38
        return $this->getDocument($record);
0 ignored issues
show
Bug introduced by
It seems like $record can also be of type string; however, parameter $record of JustSteveKing\EloquentLo...ormatter::getDocument() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
        return $this->getDocument(/** @scrutinizer ignore-type */ $record);
Loading history...
39
    }
40
41
    /**
42
     * Convert a log message into an DB Log entity
43
     *
44
     * @param  array $record
45
     * @return array
46
     */
47
    protected function getDocument(array $record)
48
    {
49
        $fills = $record['extra'];
50
        $filles['env'] = config('app.ennv');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$filles was never initialized. Although not strictly required by PHP, it is generally a good practice to add $filles = array(); before regardless.
Loading history...
51
        $fills['level'] = Str::lower($record['level_name']);
52
        $fills['message'] = $record['message'];
53
        $context = $record['context'];
54
        if (!empty($context)) {
55
            $fills['type'] = array_key_exists('type', $context) ? $context['type'] : self::LOG;
56
            $fills['result'] = array_key_exists('result', $context)  ? $context['result'] : self::NEUTRAL;
57
            $fills = array_merge($record['context'], $fills);
58
        }
59
60
        return $fills;
61
    }
62
}
63