Completed
Push — master ( 599098...2eed8d )
by Vladimir
03:27
created

Filesystem::copy()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 26
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 11
c 1
b 0
f 1
nc 7
nop 3
dl 0
loc 26
rs 8.439
ccs 0
cts 16
cp 0
crap 42
1
<?php
2
3
/**
4
 * This file contains an extended Filesystem class.
5
 *
6
 * This file is part of the Stakx project.
7
 *
8
 * @copyright 2016 Vladimir Jimenez
9
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md
10
 */
11
12
namespace allejo\stakx\System;
13
14
use Symfony\Component\Filesystem\Exception\IOException;
15
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
16
use Symfony\Component\Finder\Finder;
17
use Symfony\Component\Finder\SplFileInfo;
18
19
/**
20
 * Class Filesystem
21
 *
22
 * This class extends Symfony's Filesystem to provide convenience functions
23
 *
24
 * @package allejo\stakx\Environment
25
 */
26
class Filesystem extends \Symfony\Component\Filesystem\Filesystem
27
{
28
    /**
29
     * Build an absolute file or directory path separated by the OS specific directory separator
30
     *
31
     * @param string ...$pathFragments
32
     *
33
     * @return string
34
     */
35
    public function absolutePath ($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...
36
    {
37
        $args = func_get_args();
38
        array_unshift($args, getcwd());
39
40
        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 3
    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 3
        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 == '..') { continue; }
80
81
                $this->copy("$originFile/$entry", "$targetFile/$entry", true);
82
            }
83
84
            $dir->close();
85
        }
86
        else
87
        {
88
            parent::copy($originFile, $targetFile, $overwriteNewerFiles);
89
        }
90
    }
91
92
    /**
93
     * Strip the current working directory from an absolute path
94
     *
95
     * @param  string $path An absolute path
96
     *
97
     * @return string
98
     */
99
    public function getRelativePath ($path)
100
    {
101
        return str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path);
102
    }
103
104
    public function getFinder ($explicitIncludes = array(), $explicitIgnores = array(), $searchIn = "")
105
    {
106
        $finder = new Finder();
107
        $finder->files()
108
               ->ignoreVCS(true)
109
               ->ignoreDotFiles(true)
110
               ->ignoreUnreadableDirs();
111
112
        $finder->in(
113
            empty(trim($searchIn)) ? getcwd() : $searchIn
114
        );
115
116
        foreach ($explicitIgnores as $ignoreRule)
117
        {
118
            $isRegex = @preg_match($ignoreRule, null);
119
120
            if (substr($ignoreRule, -1, 1) === '/' || $isRegex !== false)
121
            {
122
                $finder->notPath($ignoreRule);
123
            }
124
            else
125
            {
126
                $finder->notName($ignoreRule);
127
            }
128
        }
129
130
        if (count($explicitIncludes) > 0)
131
        {
132
            foreach ($explicitIncludes as &$include)
133
            {
134
                $include = $this->absolutePath($include);
135
            }
136
137
            $finder->append($explicitIncludes);
138
        }
139
140
        return $finder;
141
    }
142
143
    /**
144
     * Get the name of a given file without the extension
145
     *
146
     * @param  string $filePath A file path
147
     *
148
     * @return string
149
     */
150 3
    public function getBaseName ($filePath)
151
    {
152 3
        return pathinfo($filePath, PATHINFO_FILENAME);
153
    }
154
155
    /**
156
     * Get the name of a given file
157
     *
158
     * @param  string $filePath A file path
159
     *
160
     * @return string
161
     */
162 2
    public function getFileName ($filePath)
163
    {
164 2
        return pathinfo($filePath, PATHINFO_BASENAME);
165
    }
166
167
    /**
168
     * Get the parent directory of a given file
169
     *
170
     * @param  string $filePath A file path
171
     *
172
     * @return string
173
     */
174 3
    public function getFolderPath ($filePath)
175
    {
176 3
        return pathinfo($filePath, PATHINFO_DIRNAME);
177
    }
178
179
    /**
180
     * Get the extension of a given file
181
     *
182
     * @param  string $filename A file path
183
     *
184
     * @return string The extension of the file
185
     */
186 23
    public function getExtension ($filename)
187
    {
188 23
        return pathinfo($filename, PATHINFO_EXTENSION);
189
    }
190
191
    /**
192
     * @param string $folderPath
193
     */
194
    public function isDir ($folderPath)
195
    {
196
        return is_dir($folderPath);
197
    }
198
199
    /**
200
     * Get the full path to the file without the extension
201
     *
202
     * @param  string $filename A file path
203
     *
204
     * @return string
205
     */
206 3
    public function removeExtension ($filename)
207
    {
208 3
        return $this->appendPath(
209 3
                   $this->getFolderPath($filename),
210 3
                   $this->getBaseName($filename)
211 3
               );
212
    }
213
214
    /**
215
     * Write a file
216
     *
217
     * @param string $targetDir The directory of where the file will be created; the file name is a separate variable
218
     * @param string $fileName  The name of the file
219
     * @param string $content   The content that belongs in the file
220
     *
221
     * @return SplFileInfo A reference to the newly created file
222
     */
223 View Code Duplication
    public function writeFile ($targetDir, $fileName, $content)
0 ignored issues
show
Duplication introduced by
This method 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...
224
    {
225
        $outputFolder = $this->getFolderPath($this->absolutePath($targetDir, $fileName));
226
        $targetFile   = $this->getFileName($fileName);
227
228
        if (!file_exists($outputFolder))
229
        {
230
            mkdir($outputFolder, 0755, true);
231
        }
232
233
        file_put_contents($this->appendPath($outputFolder, $targetFile), $content, LOCK_EX);
234
235
        return (new SplFileInfo(
236
            $fileName,
237
            $this->absolutePath($targetDir),
238
            $this->absolutePath($targetDir, $fileName))
239
        );
240
    }
241
}