Completed
Push — master ( d8fbea...c06e4f )
by Arman
28s queued 12s
created

LocalFileSystemAdapter   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 187
rs 10
wmc 22

20 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 3 2
A isDirectory() 0 3 1
A append() 0 3 1
A copy() 0 3 1
A remove() 0 3 1
A isFile() 0 3 1
A fileName() 0 3 1
A getLines() 0 3 1
A removeDirectory() 0 3 1
A isWritable() 0 3 1
A put() 0 3 1
A rename() 0 3 1
A getInstance() 0 7 2
A get() 0 3 1
A lastModified() 0 3 1
A makeDirectory() 0 3 1
A glob() 0 3 1
A isReadable() 0 3 1
A extension() 0 3 1
A size() 0 3 1
1
<?php
2
/**
3
 * Quantum PHP Framework
4
 *
5
 * An open source software development framework for PHP
6
 *
7
 * @package Quantum
8
 * @author Arman Ag. <[email protected]>
9
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
10
 * @link http://quantum.softberg.org/
11
 * @since 2.6.0
12
 */
13
14
namespace Quantum\Libraries\Storage;
15
16
/**
17
 * Class LocalFileSystemAdapter
18
 * @package Quantum\Libraries\Storage
19
 */
20
class LocalFileSystemAdapter implements FilesystemAdapterInterface
21
{
22
23
    /**
24
     * @var LocalFileSystemAdapter|null
25
     */
26
    private static $instance = null;
27
28
    /**
29
     * Get Instance
30
     * @return \Quantum\Libraries\Storage\LocalFileSystemAdapter|null
31
     */
32
    public static function getInstance(): ?LocalFileSystemAdapter
33
    {
34
        if (self::$instance === null) {
35
            self::$instance = new self();
36
        }
37
38
        return self::$instance;
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public function makeDirectory(string $dirname): bool
45
    {
46
        return mkdir($dirname);
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52
    public function removeDirectory(string $dirname): bool
53
    {
54
        return rmdir($dirname);
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60
    public function get(string $filename): string
61
    {
62
        return file_get_contents($filename);
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function put(string $filename, string $content): string
69
    {
70
        return file_put_contents($filename, $content, LOCK_EX);
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    public function append(string $filename, string $content): string
77
    {
78
        return file_put_contents($filename, $content, FILE_APPEND | LOCK_EX);
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function rename(string $oldName, string $newName): bool
85
    {
86
        return rename($oldName, $newName);
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92
    public function copy(string $source, string $dest): bool
93
    {
94
        return copy($source, $dest);
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100
    public function exists(string $filename): bool
101
    {
102
        return file_exists($filename) && is_file($filename);
103
    }
104
105
    /**
106
     * @inheritDoc
107
     */
108
    public function size(string $filename)
109
    {
110
        return filesize($filename);
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116
    public function lastModified(string $filename)
117
    {
118
        return filemtime($filename);
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124
    public function remove(string $filename): bool
125
    {
126
        return unlink($filename);
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132
    public function isFile(string $filename): bool
133
    {
134
        return is_file($filename);
135
    }
136
137
    /**
138
     * @inheritDoc
139
     */
140
    public function isDirectory(string $dirname): bool
141
    {
142
        return is_dir($dirname);
143
    }
144
145
    /**
146
     * Is Readable
147
     * @param string $filename
148
     * @return bool
149
     */
150
    public function isReadable(string $filename): bool
151
    {
152
        return is_readable($filename);
153
    }
154
155
    /**
156
     * Is Writable
157
     * @param string $filename
158
     * @return bool
159
     */
160
    public function isWritable(string $filename): bool
161
    {
162
        return is_writable($filename);
163
    }
164
165
    /**
166
     * Gets the content between given lines
167
     * @param string $filename
168
     * @param int $offset
169
     * @param int|null $length
170
     * @param int $flags
171
     * @return array
172
     */
173
    public function getLines(string $filename, int $offset, ?int $length, int $flags = 0): array
174
    {
175
        return array_slice(file($filename, $flags), $offset, $length, true);
176
    }
177
178
    /**
179
     * Gets the file name
180
     * @param string $path
181
     * @return string
182
     */
183
    public function fileName(string $path): string
184
    {
185
        return (string)pathinfo($path, PATHINFO_FILENAME);
186
    }
187
188
    /**
189
     * Gets the file extension
190
     * @param string $path
191
     * @return string
192
     */
193
    public function extension(string $path): string
194
    {
195
        return (string)pathinfo($path, PATHINFO_EXTENSION);
196
    }
197
198
    /**
199
     * Find path names matching a pattern
200
     * @param string $pattern
201
     * @param int $flags
202
     * @return array|false
203
     */
204
    public function glob(string $pattern, int $flags = 0)
205
    {
206
        return glob($pattern, $flags);
207
    }
208
209
}