Completed
Pull Request — master (#433)
by Albin
10:58
created

Local::keys()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
dl 24
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 15
nc 4
nop 0
1
<?php
2
3
namespace Gaufrette\Adapter\Local;
4
5
use Gaufrette\Util;
6
use Gaufrette\Adapter;
7
8
9
/**
10
 * Adapter for the local filesystem.
11
 *
12
 * @author Antoine Hérault <[email protected]>
13
 * @author Leszek Prabucki <[email protected]>
14
 */
15 View Code Duplication
class Local implements Adapter,
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
The first item in a multi-line implements list must be on the line following the implements keyword
Loading history...
16
                       Adapter\StreamFactory,
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 23 found
Loading history...
17
                       Adapter\ChecksumCalculator,
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 23 found
Loading history...
18
                       Adapter\SizeCalculator,
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 23 found
Loading history...
19
                       Adapter\MimeTypeProvider
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 23 found
Loading history...
20
{
21
    protected $directory;
22
    private $create;
23
    private $mode;
24
25
    /**
26
     * @param string $directory Directory where the filesystem is located
27
     * @param bool   $create    Whether to create the directory if it does not
28
     *                          exist (default FALSE)
29
     * @param int    $mode      Mode for mkdir
30
     *
31
     * @throws \RuntimeException if the specified directory does not exist and
32
     *                           could not be created
33
     */
34
    public function __construct($directory, $create = false, $mode = 0777)
35
    {
36
        // var_dump($directory);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
37
        $this->directory = Util\Path::normalize($directory);
38
        // var_dump($this->directory);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
40
        if (is_link($this->directory)) {
41
            $this->directory = realpath($this->directory);
42
        }
43
44
        $this->create = $create;
45
        $this->mode = $mode;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function read($key)
52
    {
53
        return file_get_contents($this->computePath($key));
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function write($key, $content)
60
    {
61
        $path = $this->computePath($key);
62
        $this->ensureDirectoryExists(\Gaufrette\Util\Path::dirname($path), true);
63
64
        return file_put_contents($path, $content);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function rename($sourceKey, $targetKey)
71
    {
72
        $targetPath = $this->computePath($targetKey);
73
        $this->ensureDirectoryExists(\Gaufrette\Util\Path::dirname($targetPath), true);
74
75
        return rename($this->computePath($sourceKey), $targetPath);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function exists($key)
82
    {
83
        return file_exists($this->computePath($key));
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function keys()
90
    {
91
        $this->ensureDirectoryExists($this->directory, $this->create);
92
93
        try {
94
            $files = new \RecursiveIteratorIterator(
95
                new \RecursiveDirectoryIterator(
96
                    $this->directory,
97
                    \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS
98
                ),
99
                \RecursiveIteratorIterator::CHILD_FIRST
100
            );
101
        } catch (\Exception $e) {
102
            $files = new \EmptyIterator();
103
        }
104
105
        $keys = array();
106
        foreach ($files as $file) {
107
            $keys[] = $this->computeKey($file);
108
        }
109
        sort($keys);
110
111
        return $keys;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function mtime($key)
118
    {
119
        return filemtime($this->computePath($key));
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function delete($key)
126
    {
127
        if ($this->isDirectory($key)) {
128
            return rmdir($this->computePath($key));
129
        }
130
131
        return unlink($this->computePath($key));
132
    }
133
134
    /**
135
     * @param string $key
136
     *
137
     * @return bool
138
     */
139
    public function isDirectory($key)
140
    {
141
        return is_dir($this->computePath($key));
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function createStream($key)
148
    {
149
        return new LocalStream($this->computePath($key));
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function checksum($key)
156
    {
157
        return Util\Checksum::fromFile($this->computePath($key));
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function size($key)
164
    {
165
        return Util\Size::fromFile($this->computePath($key));
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function mimeType($key)
172
    {
173
        $fileInfo = new \finfo(FILEINFO_MIME_TYPE);
174
175
        return $fileInfo->file($this->computePath($key));
176
    }
177
178
    /**
179
     * Computes the key from the specified path.
180
     *
181
     * @param string $path
182
     *
183
     * @return string
184
     */
185
    public function computeKey($path)
186
    {
187
        $path = $this->normalizePath($path);
188
189
        return ltrim(substr($path, strlen($this->directory)), '/');
190
    }
191
192
    /**
193
     * Computes the path from the specified key.
194
     *
195
     * @param string $key The key which for to compute the path
196
     *
197
     * @return string A path
198
     *
199
     * @throws \OutOfBoundsException If the computed path is out of the
200
     *                               directory
201
     * @throws \RuntimeException     If directory does not exists and cannot be created
202
     */
203
    protected function computePath($key)
204
    {
205
        $this->ensureDirectoryExists($this->directory, $this->create);
206
207
        return $this->normalizePath($this->directory.'/'.$key);
208
    }
209
210
    /**
211
     * Normalizes the given path.
212
     *
213
     * @param string $path
214
     *
215
     * @return string
216
     */
217
    protected function normalizePath($path)
218
    {
219
        $path = Util\Path::normalize($path);
220
221
        if (0 !== strpos($path, $this->directory)) {
222
            throw new \OutOfBoundsException(sprintf('The path "%s" is out of the filesystem.', $path));
223
        }
224
225
        return $path;
226
    }
227
228
    /**
229
     * Ensures the specified directory exists, creates it if it does not.
230
     *
231
     * @param string $directory Path of the directory to test
232
     * @param bool   $create    Whether to create the directory if it does
233
     *                          not exist
234
     *
235
     * @throws \RuntimeException if the directory does not exists and could not
236
     *                           be created
237
     */
238
    protected function ensureDirectoryExists($directory, $create = false)
239
    {
240
        if (!is_dir($directory)) {
241
            if (!$create) {
242
                throw new \RuntimeException(sprintf('The directory "%s" does not exist.', $directory));
243
            }
244
245
            $this->createDirectory($directory);
246
        }
247
    }
248
249
    /**
250
     * Creates the specified directory and its parents.
251
     *
252
     * @param string $directory Path of the directory to create
253
     *
254
     * @throws \InvalidArgumentException if the directory already exists
255
     * @throws \RuntimeException         if the directory could not be created
256
     */
257
    protected function createDirectory($directory)
258
    {
259
        $created = mkdir($directory, $this->mode, true);
260
261
        if (!$created) {
262
            if (!is_dir($directory)) {
263
                throw new \RuntimeException(sprintf('The directory \'%s\' could not be created.', $directory));
264
            }
265
        }
266
    }
267
}
268