Passed
Push — master ( 21afd7...3c9d18 )
by
unknown
02:32
created

LineReader   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 100
rs 10
c 0
b 0
f 0
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A LineWithoutDate() 0 3 1
A hasAtLeastOneLoggerType() 0 7 2
A isLogger() 0 12 2
A retrieveMessage() 0 3 1
A hasLoggerType() 0 9 2
A getArray() 0 6 1
A getEnv() 0 3 1
A __construct() 0 4 3
A read() 0 11 3
1
<?php
2
3
namespace Cion\LaravelLogReader\Reader;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Str;
7
8
class LineReader
9
{
10
    protected $line;
11
12
    protected $types = [
13
        'DEBUG',
14
        'EMERGENCY',
15
        'ALERT',
16
        'CRITICAL',
17
        'ERROR',
18
        'WARNING',
19
        'NOTICE',
20
        'INFO',
21
    ];
22
23
    public $type;
24
25
    public $date;
26
27
    public $message;
28
29
    public $env;
30
31
    public function __construct()
32
    {
33
        if (request()->filled('logreader_type') && request('logreader_type') !== 'all') {
34
            $this->types = [strtoupper(request()->logreader_type)];
35
        }
36
    }
37
38
    public function read($line)
39
    {
40
        $this->line = $line;
41
42
        if ($this->isLogger() && $this->hasAtLeastOneLoggerType()) {
43
            $this->retrieveMessage();
44
45
            return $this;
46
        }
47
48
        return false;
49
    }
50
51
    public function getArray()
52
    {
53
        return [
54
            'date' => $this->date,
55
            'type' => $this->type,
56
            'message' => $this->message,
57
        ];
58
    }
59
60
    public function hasAtLeastOneLoggerType()
61
    {
62
        return collect($this->types)->filter(function ($type) {
63
            if ($this->hasLoggerType($type)) {
64
                return true;
65
            }
66
        })->isNotEmpty();
67
    }
68
69
    public function isLogger()
70
    {
71
        $date = substr($this->line, 0, strpos($this->line, ']'));
72
        $date = Str::replaceFirst(']', '', $date);
73
        $date = Str::replaceFirst('[', '', $date);
74
75
        try {
76
            $this->date = Carbon::parse($date);
77
78
            return true;
79
        } catch (\Exception $e) {
80
            return false;
81
        }
82
    }
83
84
    public function LineWithoutDate()
85
    {
86
        return substr($this->line, strpos($this->line, ']') + 2);
87
    }
88
89
    public function retrieveMessage()
90
    {
91
        $this->message = Str::replaceFirst($this->getEnv().'.'.$this->type.': ', '', $this->LineWithoutDate());
92
    }
93
94
    public function hasLoggerType($type)
95
    {
96
        if (strpos($this->line, $this->getEnv().'.'.$type) !== false) {
97
            $this->type = $type;
98
99
            return true;
100
        }
101
102
        return false;
103
    }
104
105
    public function getEnv()
106
    {
107
        return config('logreader.env', env('APP_ENV'));
108
    }
109
}
110