Passed
Push — master ( bb70d5...33dd6a )
by
unknown
02:16
created

LogReader::read()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Cion\LaravelLogReader\Reader;
5
6
use Cion\LaravelLogReader\Reader\Exception\FolderNotFoundException;
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\Support\Collection;
9
use Symfony\Component\Finder\SplFileInfo;
10
11
class LogReader
12
{
13
    protected $filesystem;
14
15
    protected $files = [];
16
17
    protected $time;
18
19
    public $loggers = [];
20
21
    public function __construct()
22
    {
23
        $this->filesystem = new Filesystem;
24
    }
25
26
    public function read() : LogReader
27
    {
28
        $this->setTime();
29
30
        $this->getLogFiles()->each(function ($file) {
31
            $this->getFileLogSections($file)->each(function ($line) {
32
                $this->handleFileLine($line);
33
            });
34
        });
35
36
        return $this;
37
    }
38
39
    public function handleFileLine(array $section) : void
40
    {
41
        $sectionReader = (new LogSectionReader($section))->read();
42
43
        if (! empty($sectionReader->toArray())) {
44
            $this->loggers[] = $sectionReader->toArray();
45
        }
46
    }
47
48
    public function toArray() : array
49
    {
50
        return array_reverse($this->loggers);
51
    }
52
53
    public function toDatabase()
54
    {
55
        // specify table or model
56
    }
57
58
    public function getLogFiles() : Collection
59
    {
60
        if (! $this->filesystem->exists($this->getPath())) {
61
            throw new FolderNotFoundException();
62
        }
63
64
        return $this->getDiretoryFiles()->getSubDirectoriesFiles()->getFiles();
65
    }
66
67
    public function getFiles() : Collection
68
    {
69
        return collect($this->files)->filter(function ($file) {
70
            if ($this->hasTime()) {
71
                return $file->getFilename() === "laravel-{$this->getTime()}.log";
72
            }
73
74
            return true;
75
        });
76
    }
77
78
    public function getSubDirectoriesFiles() : LogReader
79
    {
80
        collect($this->filesystem->directories($this->getPath()))->map(function ($directory) {
81
            collect($this->filesystem->files($directory))->filter(function ($file) {
82
                $this->files[] = $file;
83
            });
84
        });
85
86
        return $this;
87
    }
88
89
    public function getDiretoryFiles() : LogReader
90
    {
91
        collect($this->filesystem->files($this->getPath()))->filter(function ($file) {
92
            $this->files[] = $file;
93
        });
94
95
        return $this;
96
    }
97
98
    public function getFileLogSections(SplFileInfo $file)
99
    {
100
        return $this->retrieveSections($this->filesystem->get($file->getPathname()));
101
    }
102
103
    public function setTime() : void
104
    {
105
        switch (request()->logreader_time) {
106
            case 'yesterday':
107
                $this->yesterday();
108
                break;
109
            case 'all':
110
                $this->all();
111
                break;
112
            default:
113
                $this->today();
114
        }
115
    }
116
117
    public function all() : LogReader
118
    {
119
        $this->time = null;
120
121
        return $this;
122
    }
123
124
    public function today() : LogReader
125
    {
126
        $this->time = now();
127
128
        return $this;
129
    }
130
131
    public function yesterday() : LogReader
132
    {
133
        $this->time = now()->yesterday();
134
135
        return $this;
136
    }
137
138
    public function hasTime() : bool
139
    {
140
        if (is_null($this->time)) {
141
            return false;
142
        }
143
144
        return true;
145
    }
146
147
    protected function getTime() : string
148
    {
149
        return $this->time->format('Y-m-d');
150
    }
151
152
    public function getPath() : string
153
    {
154
        return config('logreader.path', storage_path('logs'));
155
    }
156
157
    public function retrieveSections(string $content) : Collection
158
    {
159
        $sections = collect();
160
161
        collect(explode("\n", $content))->each(function ($line) use (&$sections) {
162
            if ((new LineHelper($line))->hasDate()) {
163
                $sections->push([
164
                    'line' => $line,
165
                    'extra' => []
166
                ]);
167
            } else {
168
                $sections = $this->addToLastSection($sections, $line);
169
            }
170
        });
171
172
        return $sections;
173
    }
174
175
    public function addToLastSection(Collection $sections, string $line)
176
    {
177
        $size = sizeof($sections);
178
179
        return $sections->map(function ($value, $key) use ($size, $line) {
180
            if ($size - 1 === $key) {
181
                $value['extra'][] = $line;
182
            }
183
184
            return $value;
185
        });
186
    }
187
}
188