LocalFileSystemAdapter::exists()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
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.5
13
 */
14
15
namespace Quantum\Libraries\Storage\Adapters\Local;
16
17
use Quantum\Libraries\Storage\Contracts\FilesystemAdapterInterface;
18
use Throwable;
19
20
/**
21
 * Class LocalFileSystemAdapter
22
 * @package Quantum\Libraries\Storage
23
 */
24
class LocalFileSystemAdapter implements FilesystemAdapterInterface
25
{
26
27
    /**
28
     * @param string $dirname
29
     * @param string|null $parentId
30
     * @inheritDoc
31
     */
32
    public function makeDirectory(string $dirname, ?string $parentId = null): bool
33
    {
34
        return mkdir($dirname);
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40
    public function removeDirectory(string $dirname): bool
41
    {
42
        if (!is_dir($dirname)) {
43
            return false;
44
        }
45
46
        return rmdir($dirname);
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52
    public function get(string $filename)
53
    {
54
        return file_get_contents($filename);
55
    }
56
57
    /**
58
     * Reads and returns the content of a file as JSON.
59
     * @param string $filename
60
     * @return false|mixed
61
     */
62
    public function getJson(string $filename)
63
    {
64
        $content = file_get_contents($filename);
65
66
        if (empty($content)) {
67
            return false;
68
        }
69
70
        $data = json_decode($content, true);
71
72
        return json_last_error() === JSON_ERROR_NONE ? $data : false;
73
    }
74
75
    /**
76
     * @param string $filename
77
     * @param string $content
78
     * @param string|null $parentId
79
     * @inheritDoc
80
     */
81
    public function put(string $filename, string $content, ?string $parentId = null)
82
    {
83
        return file_put_contents($filename, $content, LOCK_EX);
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function append(string $filename, string $content)
90
    {
91
        return file_put_contents($filename, $content, FILE_APPEND | LOCK_EX);
92
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97
    public function rename(string $oldName, string $newName): bool
98
    {
99
        return rename($oldName, $newName);
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105
    public function copy(string $source, string $dest): bool
106
    {
107
        return copy($source, $dest);
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    public function exists(string $filename): bool
114
    {
115
        return file_exists($filename) && is_file($filename);
116
    }
117
118
    /**
119
     * @inheritDoc
120
     */
121
    public function size(string $filename)
122
    {
123
        return filesize($filename);
124
    }
125
126
    /**
127
     * @inheritDoc
128
     */
129
    public function lastModified(string $filename)
130
    {
131
        return filemtime($filename);
132
    }
133
134
    /**
135
     * @inheritDoc
136
     */
137
    public function remove(string $filename): bool
138
    {
139
        return unlink($filename);
140
    }
141
142
    /**
143
     * @inheritDoc
144
     */
145
    public function isFile(string $filename): bool
146
    {
147
        return is_file($filename);
148
    }
149
150
    /**
151
     * @inheritDoc
152
     */
153
    public function isDirectory(string $dirname): bool
154
    {
155
        return is_dir($dirname);
156
    }
157
158
    /**
159
     * @inheritDoc
160
     */
161
    public function listDirectory(string $dirname)
162
    {
163
        $entries = [];
164
165
        try {
166
            foreach (scandir($dirname) as $item) {
167
                if ($item != '.' && $item != '..') {
168
                    $entries[] = realpath($dirname . DS . $item);
169
                }
170
            }
171
172
            return $entries;
173
174
        } catch (Throwable $e) {
175
            return false;
176
        }
177
    }
178
179
    /**
180
     * Find path names matching a pattern
181
     * @param string $pattern
182
     * @param int $flags
183
     * @return array|false
184
     */
185
    public function glob(string $pattern, int $flags = 0)
186
    {
187
        return glob($pattern, $flags);
188
    }
189
190
    /**
191
     * Is Readable
192
     * @param string $filename
193
     * @return bool
194
     */
195
    public function isReadable(string $filename): bool
196
    {
197
        return is_readable($filename);
198
    }
199
200
    /**
201
     * Is Writable
202
     * @param string $filename
203
     * @return bool
204
     */
205
    public function isWritable(string $filename): bool
206
    {
207
        return is_writable($filename);
208
    }
209
210
    /**
211
     * Gets the content between given lines
212
     * @param string $filename
213
     * @param int $offset
214
     * @param int|null $length
215
     * @return array
216
     */
217
    public function getLines(string $filename, int $offset = 0, ?int $length = null): array
218
    {
219
        $lines = file($filename, FILE_IGNORE_NEW_LINES);
220
221
        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...
222
            return [];
223
        }
224
225
        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...
226
            $lines = array_slice($lines, $offset, $length ?: count($lines), true);
227
        }
228
229
        return $lines;
230
    }
231
232
    /**
233
     * Gets the file name
234
     * @param string $path
235
     * @return string
236
     */
237
    public function fileName(string $path): string
238
    {
239
        return (string)pathinfo($path, PATHINFO_FILENAME);
240
    }
241
242
    /**
243
     * Gets the file extension
244
     * @param string $path
245
     * @return string
246
     */
247
    public function extension(string $path): string
248
    {
249
        return (string)pathinfo($path, PATHINFO_EXTENSION);
250
    }
251
252
    /**
253
     * Includes the required file
254
     * @param string $file
255
     * @param bool $once
256
     * @return mixed
257
     */
258
    public function require(string $file, bool $once = false)
259
    {
260
        if ($once) {
261
            return require_once $file;
262
        } else {
263
            return require $file;
264
        }
265
    }
266
267
    /**
268
     * Includes a file
269
     * @param string $file
270
     * @param bool $once
271
     * @return mixed
272
     */
273
    public function include(string $file, bool $once = false)
274
    {
275
        if ($once) {
276
            return include_once $file;
277
        } else {
278
            return include $file;
279
        }
280
    }
281
}