Completed
Pull Request — master (#67)
by ARCANEDEV
10:57
created

Filesystem::getPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
cc 1
eloc 2
nc 1
nop 0
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php namespace Arcanedev\LogViewer\Utilities;
2
3
use Arcanedev\LogViewer\Contracts\FilesystemInterface;
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 FilesystemInterface
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
     * Create a new instance.
60
     *
61
     * @param  \Illuminate\Filesystem\Filesystem  $files
62
     * @param  string                             $storagePath
63
     */
64 1368
    public function __construct(IlluminateFilesystem $files, $storagePath)
65
    {
66 1368
        $this->filesystem  = $files;
67 1368
        $this->setPath($storagePath);
68 1368
        $this->setPattern();
69 1368
    }
70
71
    /* ------------------------------------------------------------------------------------------------
72
     |  Getters & Setters
73
     | ------------------------------------------------------------------------------------------------
74
     */
75
    /**
76
     * Get the files instance.
77
     *
78
     * @return \Illuminate\Filesystem\Filesystem
79
     */
80 12
    public function getInstance()
81
    {
82 12
        return $this->filesystem;
83
    }
84
85
    /**
86
     * Set the log storage path.
87
     *
88
     * @param  string  $storagePath
89
     *
90
     * @return self
91
     */
92 1368
    public function setPath($storagePath)
93
    {
94 1368
        $this->storagePath = $storagePath;
95
96 1368
        return $this;
97
    }
98
99
    /**
100
     * Get the log pattern.
101
     *
102
     * @return string
103
     */
104 816
    public function getPattern()
105
    {
106 816
        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 self
117
     */
118 1368
    public function setPattern(
119
        $prefix    = self::PATTERN_PREFIX,
120
        $date      = self::PATTERN_DATE,
121
        $extension = self::PATTERN_EXTENSION
122
    ) {
123 1368
        $this->setPrefixPattern($prefix);
124 1368
        $this->setDatePattern($date);
125 1368
        $this->setExtension($extension);
126
127 1368
        return $this;
128
    }
129
130
    /**
131
     * Set the log date pattern.
132
     *
133
     * @param  string  $datePattern
134
     *
135
     * @return self
136
     */
137 1368
    public function setDatePattern($datePattern)
138
    {
139 1368
        $this->datePattern = $datePattern;
140
141 1368
        return $this;
142
    }
143
144
    /**
145
     * Set the log prefix pattern.
146
     *
147
     * @param  string  $prefixPattern
148
     *
149
     * @return self
150
     */
151 1368
    public function setPrefixPattern($prefixPattern)
152
    {
153 1368
        $this->prefixPattern = $prefixPattern;
154
155 1368
        return $this;
156
    }
157
158
    /**
159
     * Set the log extension.
160
     *
161
     * @param  string  $extension
162
     *
163
     * @return self
164
     */
165 1368
    public function setExtension($extension)
166
    {
167 1368
        $this->extension = $extension;
168
169 1368
        return $this;
170
    }
171
172
    /* ------------------------------------------------------------------------------------------------
173
     |  Main Functions
174
     | ------------------------------------------------------------------------------------------------
175
     */
176
    /**
177
     * Get all log files.
178
     *
179
     * @return array
180
     */
181 84
    public function all()
182
    {
183 84
        return $this->getFiles('*' . $this->extension);
184
    }
185
186
    /**
187
     * Get all valid log files.
188
     *
189
     * @return array
190
     */
191 780
    public function logs()
192
    {
193 780
        return $this->getFiles($this->getPattern());
194
    }
195
196
    /**
197
     * List the log files (Only dates).
198
     *
199
     * @param  bool|false  $withPaths
200
     *
201
     * @return array
202
     */
203 720
    public function dates($withPaths = false)
204
    {
205 720
        $files = array_reverse($this->logs());
206 720
        $dates = $this->extractDates($files);
207
208 720
        if ($withPaths) {
209 660
            $dates = array_combine($dates, $files); // [date => file]
210 495
        }
211
212 720
        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 900
    public function read($date)
225
    {
226
        try {
227 900
            $path = $this->getLogPath($date);
228
229 888
            return $this->filesystem->get($path);
230
        }
231 12
        catch (\Exception $e) {
232 12
            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 60
    public function delete($date)
246
    {
247 60
        $path = $this->getLogPath($date);
248
249
        // @codeCoverageIgnoreStart
250
        if ( ! $this->filesystem->delete($path)) {
251
            throw new FilesystemException(
252
                'There was an error deleting the log.'
253
            );
254
        }
255
        // @codeCoverageIgnoreEnd
256
257 36
        return true;
258
    }
259
260
    /**
261
     * Get the log file path.
262
     *
263
     * @param  string  $date
264
     *
265
     * @return string
266
     *
267
     * @throws \Arcanedev\LogViewer\Exceptions\FilesystemException
268
     */
269 204
    public function path($date)
270
    {
271 204
        return $this->getLogPath($date);
272
    }
273
274
    /* ------------------------------------------------------------------------------------------------
275
     |  Other Functions
276
     | ------------------------------------------------------------------------------------------------
277
     */
278
    /**
279
     * Get all files.
280
     *
281
     * @param  string  $pattern
282
     *
283
     * @return array
284
     */
285 864
    private function getFiles($pattern)
286
    {
287 864
        $files = $this->filesystem->glob(
288 864
            $this->storagePath . DS . $pattern,
289 216
            GLOB_BRACE
290 648
        );
291
292 864
        return array_filter(array_map('realpath', $files));
293
    }
294
295
    /**
296
     * Get the log file path.
297
     *
298
     * @param  string  $date
299
     *
300
     * @return string
301
     *
302
     * @throws \Arcanedev\LogViewer\Exceptions\FilesystemException
303
     */
304 972
    private function getLogPath($date)
305
    {
306 972
        $path = $this->storagePath . DS . $this->prefixPattern . $date . $this->extension;
307
308 972
        if ( ! $this->filesystem->exists($path)) {
309 36
            throw new FilesystemException(
310 9
                'The log(s) could not be located at : ' . $path
311 27
            );
312
        }
313
314 936
        return realpath($path);
315
    }
316
317
    /**
318
     * Extract dates from files.
319
     *
320
     * @param  array  $files
321
     *
322
     * @return array
323
     */
324
    private function extractDates(array $files)
325
    {
326 720
        return array_map(function ($file) {
327 720
            return extract_date(basename($file));
328 720
        }, $files);
329
    }
330
}
331