Completed
Pull Request — master (#167)
by
unknown
03:48
created

LaravelLogViewer::all()   C

Complexity

Conditions 12
Paths 26

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 6.9666
c 0
b 0
f 0
cc 12
nc 26
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 Level level
38
     */
39
    private $log_data;
40
41
    /**
42
     * @var Pattern pattern
43
     */
44
    private $pattern;
45
46
    /**
47
     * LaravelLogViewer constructor.
48
     */
49
    public function __construct()
50
    {
51
        $this->level = new Level();
52
        $this->pattern = new Pattern();
53
        $this->storage_path = function_exists('config') ? config('logviewer.storage_path',
54
            storage_path('logs')) : storage_path('logs');
55
56
    }
57
58
    /**
59
     * @param string $folder
60
     */
61
    public function setFolder($folder)
62
    {
63
        $logsPath = $this->storage_path . '/' . $folder;
64
65
        if (app('files')->exists($logsPath)) {
66
            $this->folder = $folder;
67
        }
68
    }
69
70
    /**
71
     * @param string $file
72
     * @throws \Exception
73
     */
74
    public function setFile($file)
75
    {
76
        $file = $this->pathToLogFile($file);
77
78
        if (app('files')->exists($file)) {
79
            $this->file = $file;
80
        }
81
    }
82
83
    /**
84
     * @param string $file
85
     * @return string
86
     * @throws \Exception
87
     */
88
    public function pathToLogFile($file)
89
    {
90
        $logsPath = $this->storage_path;
91
        $logsPath .= ($this->folder) ? '/' . $this->folder : '';
92
93
        if (app('files')->exists($file)) { // try the absolute path
94
            return $file;
95
        }
96
97
        $file = $logsPath . '/' . $file;
98
99
        // check if requested file is really in the logs directory
100
        if (dirname($file) !== $logsPath) {
101
            throw new \Exception('No such log file');
102
        }
103
104
        return $file;
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getFolderName()
111
    {
112
        return $this->folder;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getFileName()
119
    {
120
        return basename($this->file);
121
    }
122
123
    /**
124
     * @return array
125
     */
126
    protected function setFileAll()
127
    {
128
        if (!$this->file) {
129
            $log_file = (!$this->folder) ? $this->getFiles() : $this->getFolderFiles();
130
            if (!count($log_file)) {
131
                return [];
132
            }
133
            $this->file = $log_file[0];
134
        }
135
    }
136
137
    /**
138
     * @return array
139
     */
140
    public function all()
141
    {
142
        $log = array();
143
144
        //make sure $file is set
145
        $this->setFileAll();
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
        $this->log_data = preg_split($this->pattern->getPattern('logs'), $file);
0 ignored issues
show
Documentation Bug introduced by
It seems like preg_split($this->patter...Pattern('logs'), $file) of type array is incompatible with the declared type object<Rap2hpoutre\LaravelLogViewer\Level> of property $log_data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
160
161
        if ($this->log_data[0] < 1) {
162
            array_shift($this->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 $key => $level) {
168
                    if (strpos(strtolower($h[$i]), '.' . $level) || strpos(strtolower($h[$i]), $level . ':')) {
169
170
                        preg_match($this->pattern->getPattern('current_log',
171
                                0) . $level . $this->pattern->getPattern('current_log', 1), $h[$i], $current);
172
                        if (!isset($current[4])) {
173
                            continue;
174
                        }
175
                        $log[] = $this->getArrayLog($i, $current, $level, $key, '');
176
                    }
177
                }
178
            }
179
        }
180
181
        if (empty($log)) {
182
183
            $lines = explode(PHP_EOL, $file);
184
            $log = [];
185
186
            foreach ($lines as $key => $line) {
187
                $log[] = $this->getArrayLog('', '', '', $key, $line);
188
            }
189
        }
190
191
        return array_reverse($log);
192
    }
193
194
    /**
195
     * Create array data from log
196
     * @param $index
197
     * @param $current
198
     * @param $level
199
     * @param $key
200
     * @param $line
201
     * @return array
202
     */
203
    protected function getArrayLog($index, $current, $level, $key, $line)
204
    {
205
        return array(
206
            'context' => isset($current[3]) ? $current[3] : '',
207
            'level' => isset($level) ? $level : '',
208
            'level_class' => isset($level) ? $this->level->cssClass($level) : '',
209
            'level_img' => isset($level) ? $this->level->img($level) : '',
210
            'date' => isset($current[1]) ? $current[1] : $key + 1,
211
            'text' => isset($current[4]) ? $current[4] : $line,
212
            'in_file' => isset($current[5]) ? $current[5] : null,
213
            'stack' => preg_replace("/^\n*/", '', $this->log_data[$index])
214
        );
215
    }
216
217
    /**
218
     * @return array
219
     */
220
    public function getFolders()
221
    {
222
        $folders = glob($this->storage_path . '/*', GLOB_ONLYDIR);
223
        if (is_array($folders)) {
224
            foreach ($folders as $k => $folder) {
225
                $folders[$k] = basename($folder);
226
            }
227
        }
228
        return array_values($folders);
229
    }
230
231
    /**
232
     * @param bool $basename
233
     * @return array
234
     */
235
    public function getFolderFiles($basename = false)
236
    {
237
        return $this->getFiles($basename, $this->folder);
238
    }
239
240
    /**
241
     * @param bool $basename
242
     * @param string $folder
243
     * @return array
244
     */
245
    public function getFiles($basename = false, $folder = '')
246
    {
247
        $pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log';
248
        $files = glob($this->storage_path . '/' . $folder . '/' . $pattern,
249
            preg_match($this->pattern->getPattern('files'), $pattern) ? GLOB_BRACE : 0);
250
        $files = array_reverse($files);
251
        $files = array_filter($files, 'is_file');
252
        if ($basename && is_array($files)) {
253
            foreach ($files as $k => $file) {
254
                $files[$k] = basename($file);
255
            }
256
        }
257
        return array_values($files);
258
    }
259
}