Completed
Push — master ( 4c8845...0e974a )
by ARCANEDEV
09:55
created

Filesystem   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 311
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 311
rs 10
c 0
b 0
f 0
ccs 60
cts 60
cp 1
wmc 21
lcom 1
cbo 2

17 Methods

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