Passed
Pull Request — master (#156)
by Arman
05:40 queued 03:01
created

LocalFileSystemAdapter::lastModified()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.0
13
 */
14
15
namespace Quantum\Libraries\Storage\Adapters\Local;
16
17
use Quantum\Libraries\Storage\FilesystemAdapterInterface;
18
use Throwable;
19
20
/**
21
 * Class LocalFileSystemAdapter
22
 * @package Quantum\Libraries\Storage
23
 */
24
class LocalFileSystemAdapter implements FilesystemAdapterInterface
25
{
26
27
    /**
28
     * @var LocalFileSystemAdapter|null
29
     */
30
    private static $instance = null;
31
32
    /**
33
     * Get Instance
34
     * @return LocalFileSystemAdapter|null
35
     */
36
    public static function getInstance(): ?LocalFileSystemAdapter
37
    {
38
        if (self::$instance === null) {
39
            self::$instance = new self();
40
        }
41
42
        return self::$instance;
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function makeDirectory(string $dirname): bool
49
    {
50
        return mkdir($dirname);
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function removeDirectory(string $dirname): bool
57
    {
58
        if (!is_dir($dirname)) {
59
            return false;
60
        }
61
62
        return rmdir($dirname);
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function get(string $filename)
69
    {
70
        return file_get_contents($filename);
71
    }
72
73
    /**
74
     * Reads and returns the content of a file as JSON.
75
     * @param string $filename
76
     * @return false|mixed
77
     */
78
    public function getJson(string $filename)
79
    {
80
        $content = file_get_contents($filename);
81
82
        if (empty($content)) {
83
            return false;
84
        }
85
86
        $data = json_decode($content, true);
87
88
        return json_last_error() === JSON_ERROR_NONE ? $data : false;
89
    }
90
91
    /**
92
     * @inheritDoc
93
     */
94
    public function put(string $filename, string $content)
95
    {
96
        return file_put_contents($filename, $content, LOCK_EX);
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102
    public function append(string $filename, string $content)
103
    {
104
        return file_put_contents($filename, $content, FILE_APPEND | LOCK_EX);
105
    }
106
107
    /**
108
     * @inheritDoc
109
     */
110
    public function rename(string $oldName, string $newName): bool
111
    {
112
        return rename($oldName, $newName);
113
    }
114
115
    /**
116
     * @inheritDoc
117
     */
118
    public function copy(string $source, string $dest): bool
119
    {
120
        return copy($source, $dest);
121
    }
122
123
    /**
124
     * @inheritDoc
125
     */
126
    public function exists(string $filename): bool
127
    {
128
        return file_exists($filename) && is_file($filename);
129
    }
130
131
    /**
132
     * @inheritDoc
133
     */
134
    public function size(string $filename)
135
    {
136
        return filesize($filename);
137
    }
138
139
    /**
140
     * @inheritDoc
141
     */
142
    public function lastModified(string $filename)
143
    {
144
        return filemtime($filename);
145
    }
146
147
    /**
148
     * @inheritDoc
149
     */
150
    public function remove(string $filename): bool
151
    {
152
        return unlink($filename);
153
    }
154
155
    /**
156
     * @inheritDoc
157
     */
158
    public function isFile(string $filename): bool
159
    {
160
        return is_file($filename);
161
    }
162
163
    /**
164
     * @inheritDoc
165
     */
166
    public function isDirectory(string $dirname): bool
167
    {
168
        return is_dir($dirname);
169
    }
170
171
    /**
172
     * @inheritDoc
173
     */
174
    public function listDirectory(string $dirname)
175
    {
176
        $entries = [];
177
178
        try {
179
            foreach (scandir($dirname) as $item) {
180
                if ($item != '.' && $item != '..') {
181
                    $entries[] = realpath($dirname . DS . $item);
182
                }
183
            }
184
185
            return $entries;
186
187
        } catch (Throwable $e) {
188
            return false;
189
        }
190
    }
191
192
    /**
193
     * Find path names matching a pattern
194
     * @param string $pattern
195
     * @param int $flags
196
     * @return array|false
197
     */
198
    public function glob(string $pattern, int $flags = 0)
199
    {
200
        return glob($pattern, $flags);
201
    }
202
203
    /**
204
     * Is Readable
205
     * @param string $filename
206
     * @return bool
207
     */
208
    public function isReadable(string $filename): bool
209
    {
210
        return is_readable($filename);
211
    }
212
213
    /**
214
     * Is Writable
215
     * @param string $filename
216
     * @return bool
217
     */
218
    public function isWritable(string $filename): bool
219
    {
220
        return is_writable($filename);
221
    }
222
223
    /**
224
     * Gets the content between given lines
225
     * @param string $filename
226
     * @param int $offset
227
     * @param int|null $length
228
     * @return array
229
     */
230
    public function getLines(string $filename, int $offset = 0, ?int $length = null): array
231
    {
232
        $lines = file($filename, FILE_IGNORE_NEW_LINES);
233
234
        if(!$lines) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $lines of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
235
            return [];
236
        }
237
238
        if ($offset || $length) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $length of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
239
            $lines = array_slice($lines, $offset, $length ?: count($lines), true);
240
        }
241
242
        return $lines;
243
    }
244
245
    /**
246
     * Gets the file name
247
     * @param string $path
248
     * @return string
249
     */
250
    public function fileName(string $path): string
251
    {
252
        return (string)pathinfo($path, PATHINFO_FILENAME);
253
    }
254
255
    /**
256
     * Gets the file extension
257
     * @param string $path
258
     * @return string
259
     */
260
    public function extension(string $path): string
261
    {
262
        return (string)pathinfo($path, PATHINFO_EXTENSION);
263
    }
264
265
    /**
266
     * Includes the required file
267
     * @param string $file
268
     * @param bool $once
269
     * @return mixed
270
     */
271
    public function require(string $file, bool $once = false)
272
    {
273
        if ($once) {
274
            return require_once $file;
275
        } else {
276
            return require $file;
277
        }
278
    }
279
280
    /**
281
     * Includes a file
282
     * @param string $file
283
     * @param bool $once
284
     * @return mixed
285
     */
286
    public function include(string $file, bool $once = false)
287
    {
288
        if ($once) {
289
            return include_once $file;
290
        } else {
291
            return include $file;
292
        }
293
    }
294
295
}
296