LocalFileSystemAdapter::isWritable()   A
last analyzed

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