Completed
Pull Request — master (#272)
by
unknown
10:38
created

Filesystem::delete()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 15
ccs 4
cts 4
cp 1
crap 3
rs 9.7666
c 0
b 0
f 0
1
<?php namespace Arcanedev\LogViewer\Utilities;
2
3
use Arcanedev\LogViewer\Contracts\Utilities\Filesystem as FilesystemContract;
4
use Arcanedev\LogViewer\Exceptions\FilesystemException;
5
use Illuminate\Filesystem\Filesystem as IlluminateFilesystem;
6
7
/**
8
 * Class     Filesystem
9
 *
10
 * @package  Arcanedev\LogViewer\Utilities
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class Filesystem implements FilesystemContract
14
{
15
    /* -----------------------------------------------------------------
16
     |  Properties
17
     | -----------------------------------------------------------------
18
     */
19
20
    /**
21
     * The filesystem instance.
22
     *
23
     * @var \Illuminate\Filesystem\Filesystem
24
     */
25
    protected $filesystem;
26
27
    /**
28
     * The base storage path.
29
     *
30
     * @var string
31
     */
32
    protected $storagePath;
33
34
    /**
35
     * The log files prefix pattern.
36
     *
37
     * @var string
38
     */
39
    protected $prefixPattern;
40
41
    /**
42
     * The log files date pattern.
43
     *
44
     * @var string
45
     */
46
    protected $datePattern;
47
48
    /**
49
     * The log files extension.
50
     *
51
     * @var string
52
     */
53
    protected $extension;
54
55
    /* -----------------------------------------------------------------
56
     |  Constructor
57
     | -----------------------------------------------------------------
58
     */
59
60
    /**
61
     * Filesystem constructor.
62
     *
63
     * @param  \Illuminate\Filesystem\Filesystem  $files
64
     * @param  string                             $storagePath
65
     */
66 369
    public function __construct(IlluminateFilesystem $files, $storagePath)
67
    {
68 369
        $this->filesystem  = $files;
69 369
        $this->setPath($storagePath);
70 369
        $this->setPattern();
71 369
    }
72
73
    /* -----------------------------------------------------------------
74
     |  Getters & Setters
75
     | -----------------------------------------------------------------
76
     */
77
78
    /**
79
     * Get the files instance.
80
     *
81
     * @return \Illuminate\Filesystem\Filesystem
82
     */
83 3
    public function getInstance()
84
    {
85 3
        return $this->filesystem;
86
    }
87
88
    /**
89
     * Set the log storage path.
90
     *
91
     * @param  string  $storagePath
92
     *
93
     * @return self
94
     */
95 369
    public function setPath($storagePath)
96
    {
97 369
        $this->storagePath = $storagePath;
98
99 369
        return $this;
100
    }
101
102
    /**
103
     * Get the log pattern.
104
     *
105
     * @return string
106
     */
107 231
    public function getPattern()
108
    {
109 231
        if (config('log-viewer.parse-all-files-in-log-path')) {
110
            return "*".$this->extension;
111
        }
112
        return $this->prefixPattern.$this->datePattern.$this->extension;
113
    }
114
115
    /**
116
     * Set the log pattern.
117
     *
118
     * @param  string  $date
119
     * @param  string  $prefix
120
     * @param  string  $extension
121 369
     *
122
     * @return self
123
     */
124
    public function setPattern(
125
        $prefix    = self::PATTERN_PREFIX,
126 369
        $date      = self::PATTERN_DATE,
127 369
        $extension = self::PATTERN_EXTENSION
128 369
    ) {
129
        $this->setPrefixPattern($prefix);
130 369
        $this->setDatePattern($date);
131
        $this->setExtension($extension);
132
133
        return $this;
134
    }
135
136
    /**
137
     * Set the log date pattern.
138
     *
139
     * @param  string  $datePattern
140 369
     *
141
     * @return self
142 369
     */
143
    public function setDatePattern($datePattern)
144 369
    {
145
        $this->datePattern = $datePattern;
146
147
        return $this;
148
    }
149
150
    /**
151
     * Set the log prefix pattern.
152
     *
153
     * @param  string  $prefixPattern
154 369
     *
155
     * @return self
156 369
     */
157
    public function setPrefixPattern($prefixPattern)
158 369
    {
159
        $this->prefixPattern = $prefixPattern;
160
161
        return $this;
162
    }
163
164
    /**
165
     * Set the log extension.
166
     *
167
     * @param  string  $extension
168 369
     *
169
     * @return self
170 369
     */
171
    public function setExtension($extension)
172 369
    {
173
        $this->extension = $extension;
174
175
        return $this;
176
    }
177
178
    /* -----------------------------------------------------------------
179
     |  Main Methods
180
     | -----------------------------------------------------------------
181
     */
182
183
    /**
184
     * Get all log files.
185 21
     *
186
     * @return array
187 21
     */
188
    public function all()
189
    {
190
        return $this->getFiles('*'.$this->extension);
191
    }
192
193
    /**
194
     * Get all valid log files.
195 204
     *
196
     * @return array
197 204
     */
198
    public function logs()
199
    {
200
        return $this->getFiles($this->getPattern());
201
    }
202
203
    /**
204
     * List the log files (Only dates).
205
     *
206
     * @param  bool  $withPaths
207 189
     *
208
     * @return array
209 189
     */
210 189
    public function dates($withPaths = false)
211
    {
212 189
        $files = array_reverse($this->logs());
213 174
        $dates = $this->extractDates($files);
214
215
        if ($withPaths) {
216 189
            $dates = array_combine($dates, $files); // [date => file]
217
        }
218
219
        return $dates;
220
    }
221
222
223
    public function filenames()
224
    {
225
        $files = array_reverse($this->logs());
226
        return $files;
227
    }
228 252
229
    /**
230
     * Read the log.
231 252
     *
232 252
     * @param  string  $date
233
     *
234
     * @return string
235 3
     *
236 3
     * @throws \Arcanedev\LogViewer\Exceptions\FilesystemException
237
     */
238
    public function read($date)
239 249
    {
240
        try {
241
            $log = $this->filesystem->get(
242
                $this->getLogPath($date)
243
            );
244
        }
245
        catch (\Exception $e) {
246
            throw new FilesystemException($e->getMessage());
247
        }
248
249
        return $log;
250
    }
251 15
252
    /**
253 15
     * Delete the log.
254
     *
255
     * @param  string  $date
256
     *
257
     * @return bool
258
     *
259
     * @throws \Arcanedev\LogViewer\Exceptions\FilesystemException
260
     */
261 9
    public function delete($date)
262
    {
263
        $path = $this->getLogPath($date);
264
        if (config('log-viewer.parse-all-files-in-log-path')) {
265
            $path = $date;
266
        }
267
268
        // @codeCoverageIgnoreStart
269 3
        if ( ! $this->filesystem->delete($path)) {
270
            throw new FilesystemException('There was an error deleting the log.');
271 3
        }
272 3
        // @codeCoverageIgnoreEnd
273
274
        return true;
275
    }
276
277
    /**
278
     * Clear the log files.
279
     *
280
     * @return bool
281
     */
282
    public function clear()
283 69
    {
284
        return $this->filesystem->delete(
285 69
            $this->logs()
286
        );
287
    }
288
289
    /**
290
     * Get the log file path.
291
     *
292
     * @param  string  $date
293
     *
294
     * @return string
295
     */
296
    public function path($date)
297
    {
298
        return $this->getLogPath($date);
299
    }
300 225
301
    /* -----------------------------------------------------------------
302 225
     |  Other Methods
303 225
     | -----------------------------------------------------------------
304
     */
305
306 225
    /**
307
     * Get all files.
308
     *
309
     * @param  string  $pattern
310
     *
311
     * @return array
312
     */
313
    private function getFiles($pattern)
314
    {
315
        $files = $this->filesystem->glob(
316
            $this->storagePath.DS.$pattern, GLOB_BRACE
317
        );
318 270
319
        return array_filter(array_map('realpath', $files));
320 270
    }
321
322 270
    /**
323 9
     * Get the log file path.
324
     *
325
     * @param  string  $date
326 261
     *
327
     * @return string
328
     *
329
     * @throws \Arcanedev\LogViewer\Exceptions\FilesystemException
330
     */
331
    private function getLogPath($date)
332
    {
333
        $path = $this->storagePath.DS.$this->prefixPattern.$date.$this->extension;
334
335
        if (config('log-viewer.parse-all-files-in-log-path')) {
336 189
            $path = $date;
337
        }
338
339 189
        if ( ! $this->filesystem->exists($path)) {
340 189
            throw new FilesystemException("The log(s) could not be located at : $path");
341
        }
342
343
        return realpath($path);
344
    }
345
346
    /**
347
     * Extract dates from files.
348
     *
349
     * @param  array  $files
350
     *
351
     * @return array
352
     */
353
    private function extractDates(array $files)
354
    {
355
        return array_map(function ($file) {
356
            return extract_date(basename($file));
357
        }, $files);
358
    }
359
}
360