Completed
Pull Request — master (#269)
by
unknown
04:23
created

Filesystem::setPrefixPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
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 9
    public function getPattern()
108
    {
109 9
        return $this->prefixPattern.$this->datePattern.$this->extension;
110
    }
111
112
    /**
113
     * Set the log pattern.
114
     *
115
     * @param  string  $date
116
     * @param  string  $prefix
117
     * @param  string  $extension
118
     *
119
     * @return self
120
     */
121 369
    public function setPattern(
122
        $prefix    = self::PATTERN_PREFIX,
123
        $date      = self::PATTERN_DATE,
124
        $extension = self::PATTERN_EXTENSION
125
    ) {
126 369
        $this->setPrefixPattern($prefix);
127 369
        $this->setDatePattern($date);
128 369
        $this->setExtension($extension);
129
130 369
        return $this;
131
    }
132
133
    /**
134
     * Set the log date pattern.
135
     *
136
     * @param  string  $datePattern
137
     *
138
     * @return self
139
     */
140 369
    public function setDatePattern($datePattern)
141
    {
142 369
        $this->datePattern = $datePattern;
143
144 369
        return $this;
145
    }
146
147
    /**
148
     * Set the log prefix pattern.
149
     *
150
     * @param  string  $prefixPattern
151
     *
152
     * @return self
153
     */
154 369
    public function setPrefixPattern($prefixPattern)
155
    {
156 369
        $this->prefixPattern = $prefixPattern;
157
158 369
        return $this;
159
    }
160
161
    /**
162
     * Set the log extension.
163
     *
164
     * @param  string  $extension
165
     *
166
     * @return self
167
     */
168 369
    public function setExtension($extension)
169
    {
170 369
        $this->extension = $extension;
171
172 369
        return $this;
173
    }
174
175
    /* -----------------------------------------------------------------
176
     |  Main Methods
177
     | -----------------------------------------------------------------
178
     */
179
180
    /**
181
     * Get all log files.
182
     *
183
     * @return array
184
     */
185 21
    public function all()
186
    {
187 21
        return $this->getFiles('*'.$this->extension);
188
    }
189
190
    /**
191
     * Get all valid log files.
192
     *
193
     * @return array
194
     */
195 204
    public function logs()
196
    {
197
        //return $this->getFiles($this->getPattern());
198 204
        return $this->getFiles('*'.$this->extension);
199
    }
200
201
    /**
202
     * List the log files (Only dates).
203
     *
204
     * @param  bool  $withPaths
205
     *
206
     * @return array
207
     */
208 189
    public function dates($withPaths = false)
209
    {
210 189
        $files = array_reverse($this->logs());
211 189
        $dates = $this->extractDates($files);
212
213 189
        if ($withPaths) {
214 174
            $dates = array_combine($dates, $files); // [date => file]
0 ignored issues
show
Unused Code introduced by
$dates is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
215
        }
216
217 189
        return $files;
218
    }
219
220
    /**
221
     * Read the log.
222
     *
223
     * @param  string  $date
224
     *
225
     * @return string
226
     *
227
     * @throws \Arcanedev\LogViewer\Exceptions\FilesystemException
228
     */
229 192
    public function read($date)
230
    {
231
        try {
232 192
            $log = $this->filesystem->get(
233 192
                $this->getLogPath($date)
234
            );
235
        }
236 9
        catch (\Exception $e) {
237 9
            throw new FilesystemException($e->getMessage());
238
        }
239
240 183
        return $log;
241
    }
242
243
    /**
244
     * Delete the log.
245
     *
246
     * @param  string  $date
0 ignored issues
show
Bug introduced by
There is no parameter named $date. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
247
     *
248
     * @return bool
249
     *
250
     * @throws \Arcanedev\LogViewer\Exceptions\FilesystemException
251
     */
252
    public function delete($path)
253
    {
254
        //$path = $this->getLogPath($this->storagePath.$date);
255
256
        // @codeCoverageIgnoreStart
257
        if ( ! $this->filesystem->delete($path)) {
258
            throw new FilesystemException('There was an error deleting the log.');
259
        }
260
        // @codeCoverageIgnoreEnd
261
262
        return true;
263
    }
264
265
    /**
266
     * Clear the log files.
267
     *
268
     * @return bool
269
     */
270 3
    public function clear()
271
    {
272 3
        return $this->filesystem->delete(
273 3
            $this->logs()
274
        );
275
    }
276
277
    /**
278
     * Get the log file path.
279
     *
280
     * @param  string  $date
281
     *
282
     * @return string
283
     */
284 69
    public function path($date)
285
    {
286 69
        return $this->getLogPath($date);
287
    }
288
289
    /* -----------------------------------------------------------------
290
     |  Other Methods
291
     | -----------------------------------------------------------------
292
     */
293
294
    /**
295
     * Get all files.
296
     *
297
     * @param  string  $pattern
298
     *
299
     * @return array
300
     */
301 225
    private function getFiles($pattern)
302
    {
303 225
        $files = $this->filesystem->glob(
304 225
            $this->storagePath.DS.$pattern, GLOB_BRACE
305
        );
306
307 225
        return array_filter(array_map('realpath', $files));
308
    }
309
310
    /**
311
     * Get the log file path.
312
     *
313
     * @param  string  $date
0 ignored issues
show
Bug introduced by
There is no parameter named $date. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
314
     *
315
     * @return string
316
     *
317
     * @throws \Arcanedev\LogViewer\Exceptions\FilesystemException
318
     */
319 261
    private function getLogPath($path)
320
    {
321
        //$path = $this->storagePath.DS.$this->prefixPattern.$date.$this->extension;
322
323 261
        if ( ! $this->filesystem->exists($path)) {
324 78
            throw new FilesystemException("The log(s) could not be located at : $path");
325
        }
326
327 183
        return realpath($path);
328
    }
329
330
    /**
331
     * Extract dates from files.
332
     *
333
     * @param  array  $files
334
     *
335
     * @return array
336
     */
337 189
    private function extractDates(array $files)
338
    {
339
        return array_map(function ($file) {
340 189
            return extract_date(basename($file));
341 189
        }, $files);
342
    }
343
}
344