Completed
Pull Request — master (#175)
by Elminson
09:16
created

LaravelLogViewer::all()   D

Complexity

Conditions 16
Paths 91

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 4.9333
c 0
b 0
f 0
cc 16
nc 91
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Rap2hpoutre\LaravelLogViewer;
4
5
/**
6
 * Class LaravelLogViewer
7
 * @package Rap2hpoutre\LaravelLogViewer
8
 */
9
class LaravelLogViewer
10
{
11
    /**
12
     * @var string file
13
     */
14
    private $file;
15
16
    /**
17
     * @var string folder
18
     */
19
    private $folder;
20
21
    /**
22
     * @var string storage_path
23
     */
24
    private $storage_path;
25
26
    /**
27
     * Why? Uh... Sorry
28
     */
29
    const MAX_FILE_SIZE = 52428800;
30
31
    /**
32
     * @var Level level
33
     */
34
    private $level;
35
36
    /**
37
     * @var Pattern pattern
38
     */
39
    private $pattern;
40
41
    /**
42
     * LaravelLogViewer constructor.
43
     */
44
    public function __construct()
45
    {
46
        $this->level = new Level();
47
        $this->pattern = new Pattern();
48
        $this->storage_path = function_exists('config') ? config('logviewer.storage_path', storage_path('logs')) : storage_path('logs');
49
50
    }
51
52
    /**
53
     * @param string $folder
54
     */
55
    public function setFolder($folder)
56
    {
57
        //fix multi level folder
58
        if(is_array($this->storage_path)){
59 View Code Duplication
            foreach ($this->storage_path as $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
                $logsPath = $value . '/' . $folder;
61
                if (app('files')->exists($logsPath)) {
62
63
                    $this->folder = $folder;
64
                    break;
65
                }
66
            }
67
        } else {
68
            if (app('files')->exists($folder)) {
69
                $this->folder = $folder;
70
            }
71
        }
72
    }
73
74
    /**
75
     * @param string $file
76
     * @throws \Exception
77
     */
78
    public function setFile($file)
79
    {
80
        $file = $this->pathToLogFile($file);
81
82
        if (app('files')->exists($file)) {
83
            $this->file = $file;
84
        }
85
    }
86
87
    /**
88
     * @param string $file
89
     * @return string
90
     * @throws \Exception
91
     */
92
    public function pathToLogFile($file)
93
    {
94
        if (app('files')->exists($file)) { // try the absolute path
95
            return $file;
96
        }
97
        if (is_array($this->storage_path)) {
98 View Code Duplication
            foreach ($this->storage_path as $folder) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
                if (app('files')->exists($folder . '/' . $file)) { // try the absolute path
100
                    $file = $folder . '/' . $file;
101
                    break;
102
                }
103
            }
104
        } else {
105
            $logsPath = $this->storage_path;
106
            $logsPath .= ($this->folder) ? '/' . $this->folder : '';
107
            $file = $logsPath . '/' . $file;
108
            // check if requested file is really in the logs directory
109
            if (dirname($file) !== $logsPath) {
110
                throw new \Exception('No such log file');
111
            }
112
        }
113
        return $file;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getFolderName()
120
    {
121
        return $this->folder;
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public function getFileName()
128
    {
129
        return basename($this->file);
130
    }
131
132
    /**
133
     * @return array
134
     */
135
    public function all()
136
    {
137
        $log = array();
138
139
        if (!$this->file) {
140
            $log_file = (!$this->folder) ? $this->getFiles() : $this->getFolderFiles();
141
            if (!count($log_file)) {
142
                return [];
143
            }
144
            $this->file = $log_file[0];
145
        }
146
147
        if (app('files')->size($this->file) > self::MAX_FILE_SIZE) {
148
            return null;
149
        }
150
151
        $file = app('files')->get($this->file);
152
153
        preg_match_all($this->pattern->getPattern('logs'), $file, $headings);
154
155
        if (!is_array($headings)) {
156
            return $log;
157
        }
158
159
        $log_data = preg_split($this->pattern->getPattern('logs'), $file);
160
161
        if ($log_data[0] < 1) {
162
            array_shift($log_data);
163
        }
164
165
        foreach ($headings as $h) {
166
            for ($i = 0, $j = count($h); $i < $j; $i++) {
167
                foreach ($this->level->all() as $level) {
168
                    if (strpos(strtolower($h[$i]), '.' . $level) || strpos(strtolower($h[$i]), $level . ':')) {
169
170
                        preg_match($this->pattern->getPattern('current_log', 0) . $level . $this->pattern->getPattern('current_log', 1), $h[$i], $current);
171
                        if (!isset($current[4])) {
172
                            continue;
173
                        }
174
175
                        $log[] = array(
176
                            'context' => $current[3],
177
                            'level' => $level,
178
                            'folder' => $this->folder,
179
                            'level_class' => $this->level->cssClass($level),
180
                            'level_img' => $this->level->img($level),
181
                            'date' => $current[1],
182
                            'text' => $current[4],
183
                            'in_file' => isset($current[5]) ? $current[5] : null,
184
                            'stack' => preg_replace("/^\n*/", '', $log_data[$i])
185
                        );
186
                    }
187
                }
188
            }
189
        }
190
191
        if (empty($log)) {
192
193
            $lines = explode(PHP_EOL, $file);
194
            $log = [];
195
196
            foreach ($lines as $key => $line) {
197
                $log[] = [
198
                    'context' => '',
199
                    'level' => '',
200
                    'folder' => '',
201
                    'level_class' => '',
202
                    'level_img' => '',
203
                    'date' => $key + 1,
204
                    'text' => $line,
205
                    'in_file' => null,
206
                    'stack' => '',
207
                ];
208
            }
209
        }
210
211
        return array_reverse($log);
212
    }
213
214
    /**
215
     * @return array
216
     */
217
    public function getFolders()
218
    {
219
        $folders = [];
220
        if (is_array($this->storage_path)) {
221
            foreach ($this->storage_path as $value) {
222
                $folders = array_merge(
223
                  $folders,
224
                  glob($value . '/*', GLOB_ONLYDIR)
225
                );
226
            }
227
        } else {
228
            $folders = glob($this->storage_path . '/*', GLOB_ONLYDIR);
229
        }
230
231
        if (is_array($folders)) {
232
            foreach ($folders as $k => $folder) {
233
                $folders[$k] = basename($folder);
234
            }
235
        }
236
        return array_values($folders);
237
    }
238
239
    /**
240
     * @param bool $basename
241
     * @return array
242
     */
243
    public function getFolderFiles($basename = false)
244
    {
245
        return $this->getFiles($basename, $this->folder);
246
    }
247
248
    /**
249
     * @param bool $basename
250
     * @param string $folder
251
     * @return array
252
     */
253
    public function getFiles($basename = false, $folder = '')
254
    {
255
        $files = [];
256
        $pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log';
257
        if (is_array($this->storage_path)) {
258
            foreach ($this->storage_path as $value) {
259
                $files = array_merge(
260
                  $files,
261
                  glob(
262
                    $value . '/' . $folder . '/' . $pattern,
263
                    preg_match($this->pattern->getPattern('files'), $pattern) ? GLOB_BRACE : 0
264
                  )
265
                );
266
            }
267
        } else {
268
            $files = glob(
269
              $this->storage_path . '/' . $folder . '/' . $pattern,
270
              preg_match($this->pattern->getPattern('files'), $pattern) ? GLOB_BRACE : 0
271
            );
272
        }
273
274
        $files = array_reverse($files);
275
        $files = array_filter($files, 'is_file');
276
        if ($basename && is_array($files)) {
277
            foreach ($files as $k => $file) {
278
                $files[$k] = basename($file);
279
            }
280
        }
281
        return array_values($files);
282
    }
283
}
284