Completed
Push — master ( f35125...dab7b5 )
by Vladimir
04:21 queued 01:46
created

Filesystem::absolutePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.5

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
ccs 3
cts 6
cp 0.5
crap 2.5
rs 9.4285
c 0
b 0
f 0
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\SplFileInfo;
17
18
/**
19
 * Class Filesystem
20
 *
21
 * This class extends Symfony's Filesystem to provide convenience functions
22
 *
23
 * @package allejo\stakx\Environment
24
 */
25
class Filesystem extends \Symfony\Component\Filesystem\Filesystem
26
{
27
    /**
28
     * Build an absolute file or directory path separated by the OS specific directory separator
29
     *
30
     * @param string ...$pathFragments
31
     *
32
     * @return string
33
     */
34 4
    public function absolutePath ($pathFragments)
35
    {
36 4
        if ($this->isAbsolutePath($pathFragments))
37
        {
38 4
            return $pathFragments;
39
        }
40
41
        $args = func_get_args();
42
        array_unshift($args, getcwd());
43
44
        return implode(DIRECTORY_SEPARATOR, $args);
45
    }
46
47
    /**
48
     * Build a file or directory path separated by the OS specific directory separator
49
     *
50
     * @param string ...$pathFragments
51
     *
52
     * @return string
53
     */
54 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...
55
    {
56 3
        return implode(DIRECTORY_SEPARATOR, func_get_args());
57
    }
58
59
    /**
60
     * Copy a file or folder recursively
61
     *
62
     * @param string $originFile          The original filename
63
     * @param string $targetFile          The target filename
64
     * @param bool   $overwriteNewerFiles If true, target files newer than origin files are overwritten
65
     *
66
     * @throws FileNotFoundException When originFile doesn't exist
67
     * @throws IOException           When copy fails
68
     */
69
    public function copy($originFile, $targetFile, $overwriteNewerFiles = false)
70
    {
71
        if ($this->isDir($originFile))
72
        {
73
            if (!$this->isDir($targetFile))
74
            {
75
                mkdir($targetFile, 0755, true);
76
            }
77
78
            $dir = dir($originFile);
79
80
            while (false !== $entry = $dir->read())
81
            {
82
                // Skip pointers
83
                if ($entry == '.' || $entry == '..') { continue; }
84
85
                $this->copy("$originFile/$entry", "$targetFile/$entry", true);
86
            }
87
88
            $dir->close();
89
        }
90
        else
91
        {
92
            parent::copy($originFile, $targetFile, $overwriteNewerFiles);
93
        }
94
    }
95
96
    /**
97
     * Strip the current working directory from an absolute path
98
     *
99
     * @param  string $path An absolute path
100
     *
101
     * @return string
102
     */
103 4
    public function getRelativePath ($path)
104
    {
105 4
        return str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path);
106
    }
107
108
    /**
109
     * Get the name of a given file without the extension
110
     *
111
     * @param  string $filePath A file path
112
     *
113
     * @return string
114
     */
115 7
    public function getBaseName ($filePath)
116
    {
117 7
        return pathinfo($filePath, PATHINFO_FILENAME);
118
    }
119
120
    /**
121
     * Get the name of a given file
122
     *
123
     * @param  string $filePath A file path
124
     *
125
     * @return string
126
     */
127 2
    public function getFileName ($filePath)
128
    {
129 2
        return pathinfo($filePath, PATHINFO_BASENAME);
130
    }
131
132
    /**
133
     * Get the parent directory of a given file
134
     *
135
     * @param  string $filePath A file path
136
     *
137
     * @return string
138
     */
139 3
    public function getFolderPath ($filePath)
140
    {
141 3
        return pathinfo($filePath, PATHINFO_DIRNAME);
142
    }
143
144
    /**
145
     * Get the extension of a given file
146
     *
147
     * @param  string $filename A file path
148
     *
149
     * @return string The extension of the file
150
     */
151 27
    public function getExtension ($filename)
152
    {
153 27
        return pathinfo($filename, PATHINFO_EXTENSION);
154
    }
155
156
    /**
157
     * @param string $folderPath
158
     */
159
    public function isDir ($folderPath)
160
    {
161
        return is_dir($folderPath);
162
    }
163
164
    /**
165
     * Get the full path to the file without the extension
166
     *
167
     * @param  string $filename A file path
168
     *
169
     * @return string
170
     */
171 3
    public function removeExtension ($filename)
172
    {
173 3
        return $this->appendPath(
174 3
                   $this->getFolderPath($filename),
175 3
                   $this->getBaseName($filename)
176
               );
177
    }
178
179
    /**
180
     * Write a file
181
     *
182
     * @param string $targetDir The directory of where the file will be created; the file name is a separate variable
183
     * @param string $fileName  The name of the file
184
     * @param string $content   The content that belongs in the file
185
     *
186
     * @return SplFileInfo A reference to the newly created file
187
     */
188 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...
189
    {
190
        $outputFolder = $this->getFolderPath($this->absolutePath($targetDir, $fileName));
191
        $targetFile   = $this->getFileName($fileName);
192
193
        if (!file_exists($outputFolder))
194
        {
195
            mkdir($outputFolder, 0755, true);
196
        }
197
198
        file_put_contents($this->appendPath($outputFolder, $targetFile), $content, LOCK_EX);
199
200
        return (new SplFileInfo(
201
            $fileName,
202
            $this->absolutePath($targetDir),
203
            $this->absolutePath($targetDir, $fileName))
204
        );
205
    }
206
}