LogReader::handleFileLine()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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