Completed
Push — master ( 67b389...a41cf5 )
by Vladimir
05:14
created

Filesystem   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 232
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 61.82%

Importance

Changes 0
Metric Value
wmc 23
lcom 2
cbo 5
dl 0
loc 232
ccs 34
cts 55
cp 0.6182
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A appendPath() 0 4 1
A absolutePath() 0 12 2
B copy() 0 29 6
A createFileObject() 0 4 1
A getRelativePath() 0 4 1
A getBaseName() 0 4 1
A getFileName() 0 4 1
A getFolderPath() 0 4 1
A getExtension() 0 4 1
A isDir() 0 4 1
A isFile() 0 4 1
A isSymlink() 0 4 1
A path() 0 4 1
A safeReadFile() 0 22 3
A removeExtension() 0 7 1
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\System;
9
10
use allejo\stakx\Exception\FileAccessDeniedException;
11
use allejo\stakx\Filesystem\File;
12
use allejo\stakx\Filesystem\FilesystemPath;
13
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
14
use Symfony\Component\Filesystem\Exception\IOException;
15
16
/**
17
 * Class Filesystem.
18
 *
19
 * This class extends Symfony's Filesystem to provide convenience functions
20
 */
21
class Filesystem extends \Symfony\Component\Filesystem\Filesystem
22
{
23
    /**
24
     * Build an absolute file or directory path separated by the OS specific directory separator.
25
     *
26
     * @param string ...$pathFragments
27
     *
28
     * @return string
29
     */
30 88
    public function absolutePath($pathFragments)
31
    {
32 88
        if ($this->isAbsolutePath($pathFragments))
33
        {
34 47
            return $pathFragments;
35
        }
36
37 69
        $args = func_get_args();
38 69
        array_unshift($args, getcwd());
39
40 69
        return implode(DIRECTORY_SEPARATOR, $args);
41
    }
42
43
    /**
44
     * Build a file or directory path separated by the OS specific directory separator.
45
     *
46
     * @param string ...$pathFragments
47
     *
48
     * @return string
49
     */
50 58
    public function appendPath($pathFragments)
0 ignored issues
show
Unused Code introduced by
The parameter $pathFragments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
    {
52 58
        return implode(DIRECTORY_SEPARATOR, func_get_args());
53
    }
54
55
    /**
56
     * Copy a file or folder recursively.
57
     *
58
     * @param string $originFile          The original filename
59
     * @param string $targetFile          The target filename
60
     * @param bool   $overwriteNewerFiles If true, target files newer than origin files are overwritten
61
     *
62
     * @throws FileNotFoundException When originFile doesn't exist
63
     * @throws IOException           When copy fails
64
     */
65
    public function copy($originFile, $targetFile, $overwriteNewerFiles = false)
66
    {
67
        if ($this->isDir($originFile))
68
        {
69
            if (!$this->isDir($targetFile))
70
            {
71
                mkdir($targetFile, 0755, true);
72
            }
73
74
            $dir = dir($originFile);
75
76
            while (false !== $entry = $dir->read())
77
            {
78
                // Skip pointers
79
                if ($entry == '.' || $entry == '..')
80
                {
81
                    continue;
82
                }
83
84
                $this->copy("$originFile/$entry", "$targetFile/$entry", true);
85
            }
86
87
            $dir->close();
88
        }
89
        else
90
        {
91
            parent::copy($originFile, $targetFile, $overwriteNewerFiles);
92
        }
93
    }
94
95
    /**
96
     * Create an instance of stakx's File object with relative path information.
97
     *
98
     * @param string $filePath
99
     *
100
     * @return File
101
     */
102
    public function createFileObject($filePath)
103
    {
104
        return new File($this->absolutePath($filePath));
105
    }
106
107
    /**
108
     * Strip the current working directory from an absolute path.
109
     *
110
     * @param string $path An absolute path
111
     *
112
     * @return string
113
     */
114 89
    public function getRelativePath($path)
115
    {
116 89
        return str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path);
117
    }
118
119
    /**
120
     * Get the name of a given file without the extension.
121
     *
122
     * @param string $filePath A file path
123
     *
124
     * @return string
125
     */
126 4
    public function getBaseName($filePath)
127
    {
128 4
        return pathinfo($filePath, PATHINFO_FILENAME);
129
    }
130
131
    /**
132
     * Get the name of a given file.
133
     *
134
     * @param string $filePath A file path
135
     *
136
     * @return string
137
     */
138
    public function getFileName($filePath)
139
    {
140
        return pathinfo($filePath, PATHINFO_BASENAME);
141
    }
142
143
    /**
144
     * Get the parent directory of a given file.
145
     *
146
     * @param string $filePath A file path
147
     *
148
     * @return string
149
     */
150 40
    public function getFolderPath($filePath)
151
    {
152 40
        return pathinfo($filePath, PATHINFO_DIRNAME);
153
    }
154
155
    /**
156
     * Get the extension of a given file.
157
     *
158
     * @param string $filename A file path
159
     *
160
     * @return string The extension of the file
161
     */
162 48
    public function getExtension($filename)
163
    {
164 48
        return pathinfo($filename, PATHINFO_EXTENSION);
165
    }
166
167
    /**
168
     * Check whether or not if a given path is a directory.
169
     *
170
     * @param string $folderPath
171
     *
172
     * @return bool
173
     */
174 12
    public function isDir($folderPath)
175
    {
176 12
        return is_dir($folderPath);
177
    }
178
179
    /**
180
     * Check whether or not a given path is a file.
181
     *
182
     * @param string $filePath
183
     *
184
     * @return bool
185
     */
186
    public function isFile($filePath)
187
    {
188
        return is_file($filePath);
189
    }
190
191
    /**
192
     * Check whether a given file path is a symlink
193
     *
194
     * @param  string $filePath
195
     *
196
     * @return bool
197
     */
198 11
    public function isSymlink($filePath)
199
    {
200 11
        return is_link($filePath);
201
    }
202
203
    /**
204
     * Only read a file's contents if it's within the current working directory
205
     *
206
     * @param  string $filePath
207
     *
208
     * @return bool|string
209
     */
210 36
    public function safeReadFile($filePath)
211
    {
212 36
        $absPath = realpath($this->absolutePath($filePath));
213
214 36
        if (!$this->exists($absPath))
215
        {
216 1
            throw new FileNotFoundException(sprintf(
217 1
                "The '%s' file could not be found or is outside the website working directory",
218 1
                $filePath
219
            ));
220
        }
221
222 36
        if (strpos($absPath, getcwd()) !== 0)
223
        {
224
            throw new FileAccessDeniedException(sprintf(
225
                "The '%s' file is outside the website working directory",
226
                $filePath
227
            ));
228
        }
229
230 36
        return file_get_contents($absPath);
231
    }
232
233
    /**
234
     * Get the full path to the file without the extension.
235
     *
236
     * @param string $filename A file path
237
     *
238
     * @return string
239
     */
240 4
    public function removeExtension($filename)
241
    {
242 4
        return $this->appendPath(
243 4
            $this->getFolderPath($filename),
244 4
            $this->getBaseName($filename)
245
        );
246
    }
247
248 2
    public function path($path)
249
    {
250 2
        return (new FilesystemPath($path));
251
    }
252
}
253