LogSectionReader   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 17

8 Methods

Rating   Name   Duplication   Size   Complexity  
A handleExtra() 0 9 1
A toArray() 0 11 5
A retrieveSectionInformations() 0 9 1
A handleDate() 0 4 2
A read() 0 5 1
A __construct() 0 5 1
A handleMessage() 0 3 1
A handleType() 0 9 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cion\LaravelLogReader\Reader;
6
7
use Carbon\Carbon;
8
9
class LogSectionReader
10
{
11
    protected $lineHelper;
12
13
    protected $types = [
14
        'DEBUG',
15
        'EMERGENCY',
16
        'ALERT',
17
        'CRITICAL',
18
        'ERROR',
19
        'WARNING',
20
        'NOTICE',
21
        'INFO',
22
    ];
23
24
    public $type;
25
26
    public $date;
27
28
    public $message;
29
30
    public $extra;
31
32
    public function __construct(array $section)
33
    {
34
        $this->lineHelper = new LineHelper($section['line']);
35
36
        $this->extra = collect($section['extra']);
37
    }
38
39
    public function read() : self
40
    {
41
        $this->retrieveSectionInformations();
42
43
        return $this;
44
    }
45
46
    public function handleType() : void
47
    {
48
        if (request()->filled('logreader_type') && request('logreader_type') !== 'all') {
49
            $this->types = [strtoupper(request()->logreader_type)];
50
        }
51
52
        foreach ($this->types as $type) {
53
            if ($this->lineHelper->hasType($type)) {
54
                $this->type = $type;
55
            }
56
        }
57
    }
58
59
    public function handleDate() : void
60
    {
61
        if (strtotime($date = $this->lineHelper->getDate())) {
62
            $this->date = Carbon::parse($date);
63
        }
64
    }
65
66
    public function handleMessage() : void
67
    {
68
        $this->message = $this->lineHelper->getLogMessageOf($this->type);
69
    }
70
71
    public function retrieveSectionInformations() : void
72
    {
73
        $this->handleDate();
74
75
        $this->handleType();
76
77
        $this->handleMessage();
78
79
        $this->handleExtra();
80
    }
81
82
    public function handleExtra() : void
83
    {
84
        // Remove Useless Informations
85
        $this->extra->shift();
86
        $this->extra->pop();
87
        $this->extra->pop();
88
89
        $this->extra = $this->extra->map(function ($extra) {
90
            return explode(': ', substr($extra, 3));
91
        });
92
    }
93
94
    public function toArray() : array
95
    {
96
        if (! $this->date || ! $this->type || ! $this->message || ! $this->extra) {
97
            return [];
98
        }
99
100
        return [
101
            'date' => $this->date,
102
            'type' => $this->type,
103
            'message' => $this->message,
104
            'extra' => $this->extra->toArray(),
105
        ];
106
    }
107
}
108