Completed
Pull Request — master (#42)
by Vladimir
02:38
created

Filesystem   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 211
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 61.22%

Importance

Changes 0
Metric Value
dl 0
loc 211
ccs 30
cts 49
cp 0.6122
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 3

13 Methods

Rating   Name   Duplication   Size   Complexity  
A appendPath() 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 absolutePath() 0 12 2
B copy() 0 29 6
A createSplFileInfo() 0 8 1
A isSymlink() 0 4 1
A safeReadFile() 0 14 2
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 Symfony\Component\Filesystem\Exception\FileNotFoundException;
11
use Symfony\Component\Filesystem\Exception\IOException;
12
use Symfony\Component\Finder\SplFileInfo;
13
14
/**
15
 * Class Filesystem.
16
 *
17
 * This class extends Symfony's Filesystem to provide convenience functions
18
 */
19
class Filesystem extends \Symfony\Component\Filesystem\Filesystem
20
{
21
    /**
22
     * Build an absolute file or directory path separated by the OS specific directory separator.
23
     *
24
     * @param string ...$pathFragments
25
     *
26
     * @return string
27
     */
28 80
    public function absolutePath($pathFragments)
29
    {
30 80
        if ($this->isAbsolutePath($pathFragments))
31
        {
32 53
            return $pathFragments;
33
        }
34
35 60
        $args = func_get_args();
36 60
        array_unshift($args, getcwd());
37
38 60
        return implode(DIRECTORY_SEPARATOR, $args);
39
    }
40
41
    /**
42
     * Build a file or directory path separated by the OS specific directory separator.
43
     *
44
     * @param string ...$pathFragments
45
     *
46
     * @return string
47
     */
48 49
    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...
49
    {
50 49
        return implode(DIRECTORY_SEPARATOR, func_get_args());
51
    }
52
53
    /**
54
     * Copy a file or folder recursively.
55
     *
56
     * @param string $originFile          The original filename
57
     * @param string $targetFile          The target filename
58
     * @param bool   $overwriteNewerFiles If true, target files newer than origin files are overwritten
59
     *
60
     * @throws FileNotFoundException When originFile doesn't exist
61
     * @throws IOException           When copy fails
62
     */
63
    public function copy($originFile, $targetFile, $overwriteNewerFiles = false)
64
    {
65
        if ($this->isDir($originFile))
66
        {
67
            if (!$this->isDir($targetFile))
68
            {
69
                mkdir($targetFile, 0755, true);
70
            }
71
72
            $dir = dir($originFile);
73
74
            while (false !== $entry = $dir->read())
75
            {
76
                // Skip pointers
77
                if ($entry == '.' || $entry == '..')
78
                {
79
                    continue;
80
                }
81
82
                $this->copy("$originFile/$entry", "$targetFile/$entry", true);
3 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $originFile instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $entry instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $targetFile instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
83
            }
84
85
            $dir->close();
86
        }
87
        else
88
        {
89
            parent::copy($originFile, $targetFile, $overwriteNewerFiles);
90
        }
91
    }
92
93
    /**
94
     * Create an instance of Symfony's SplFileInfo with relative path information.
95
     *
96
     * @param string $filePath
97
     *
98
     * @return SplFileInfo
99
     */
100
    public function createSplFileInfo($filePath)
101
    {
102
        return new SplFileInfo(
103
            $this->absolutePath($filePath),
104
            $this->getRelativePath($this->getFolderPath($filePath)),
105
            $this->getRelativePath($filePath)
106
        );
107
    }
108
109
    /**
110
     * Strip the current working directory from an absolute path.
111
     *
112
     * @param string $path An absolute path
113
     *
114
     * @return string
115
     */
116 79
    public function getRelativePath($path)
117
    {
118 79
        return str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path);
119
    }
120
121
    /**
122
     * Get the name of a given file without the extension.
123
     *
124
     * @param string $filePath A file path
125
     *
126
     * @return string
127
     */
128 39
    public function getBaseName($filePath)
129
    {
130 39
        return pathinfo($filePath, PATHINFO_FILENAME);
131
    }
132
133
    /**
134
     * Get the name of a given file.
135
     *
136
     * @param string $filePath A file path
137
     *
138
     * @return string
139
     */
140 29
    public function getFileName($filePath)
141
    {
142 29
        return pathinfo($filePath, PATHINFO_BASENAME);
143
    }
144
145
    /**
146
     * Get the parent directory of a given file.
147
     *
148
     * @param string $filePath A file path
149
     *
150
     * @return string
151
     */
152 33
    public function getFolderPath($filePath)
153
    {
154 33
        return pathinfo($filePath, PATHINFO_DIRNAME);
155
    }
156
157
    /**
158
     * Get the extension of a given file.
159
     *
160
     * @param string $filename A file path
161
     *
162
     * @return string The extension of the file
163
     */
164 120
    public function getExtension($filename)
165
    {
166 120
        return pathinfo($filename, PATHINFO_EXTENSION);
167
    }
168
169
    /**
170
     * Check whether or not if a given path is a directory.
171
     *
172
     * @param string $folderPath
173
     *
174
     * @return bool
175
     */
176 29
    public function isDir($folderPath)
177
    {
178 29
        return is_dir($folderPath);
179
    }
180
181
    /**
182
     * Check whether a given file path is a symlink
183
     *
184
     * @param  string $filePath
185
     *
186
     * @return bool
187
     */
188 9
    public function isSymlink($filePath)
189
    {
190 9
        return is_link($filePath);
191
    }
192
193
    /**
194
     * Only read a file's contents if it's within the current working directory
195
     *
196
     * @param  string $filePath
197
     *
198
     * @return bool|string
199
     */
200 30
    public function safeReadFile($filePath)
201
    {
202 30
        $absPath = realpath($this->absolutePath($filePath));
203
204 30
        if (strpos($absPath, getcwd()) !== 0)
205
        {
206
            throw new FileNotFoundException(sprintf(
207
                "The '%s' file could not be found or is outside the website working directory",
208
                $filePath
209
            ));
210
        }
211
212 30
        return file_get_contents($absPath);
213
    }
214
215
    /**
216
     * Get the full path to the file without the extension.
217
     *
218
     * @param string $filename A file path
219
     *
220
     * @return string
221
     */
222 4
    public function removeExtension($filename)
223
    {
224 4
        return $this->appendPath(
225 4
            $this->getFolderPath($filename),
226 4
            $this->getBaseName($filename)
227
        );
228
    }
229
}
230