Completed
Push — master ( e6222a...4bc239 )
by Vladimir
02:26
created

Filesystem::removeExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 4
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 7
rs 9.4285
ccs 0
cts 5
cp 0
crap 2
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\Finder\Finder;
15
use Symfony\Component\Finder\SplFileInfo;
16
17
/**
18
 * Class Filesystem
19
 *
20
 * This class extends Symfony's Filesystem to provide convenience functions
21
 *
22
 * @package allejo\stakx\Environment
23
 */
24
class Filesystem extends \Symfony\Component\Filesystem\Filesystem
25
{
26
    /**
27
     * Build an absolute file or directory path separated by the OS specific directory separator
28
     *
29
     * @param string ...$pathFragments
30
     *
31
     * @return string
32
     */
33
    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...
34
    {
35
        $args = func_get_args();
36
        array_unshift($args, getcwd());
37
38
        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
    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
        return implode(DIRECTORY_SEPARATOR, func_get_args());
51
    }
52
53
    /**
54
     * Strip the current working directory from an absolute path
55
     *
56
     * @param  string $path An absolute path
57
     *
58
     * @return string
59
     */
60
    public function getRelativePath ($path)
61
    {
62
        return str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path);
63
    }
64
65
    public function getFinder ($explicitIncludes = array(), $explicitIgnores = array(), $searchIn = "")
66
    {
67
        $finder = new Finder();
68
        $finder->files()
69
               ->ignoreVCS(true)
70
               ->ignoreDotFiles(true)
71
               ->ignoreUnreadableDirs();
72
73
        $finder->in(
74
            empty(trim($searchIn)) ? getcwd() : $searchIn
75
        );
76
77
        foreach ($explicitIgnores as $ignoreRule)
78
        {
79
            $finder->notPath($ignoreRule);
80
        }
81
82
        if (count($explicitIncludes) > 0)
83
        {
84
            foreach ($explicitIncludes as &$include)
85
            {
86
                $include = $this->absolutePath($include);
87
            }
88
89
            $finder->append($explicitIncludes);
90
        }
91
92
        return $finder;
93
    }
94
95
    /**
96
     * Get the name of a given file without the extension
97
     *
98
     * @param  string $filePath A file path
99
     *
100
     * @return string
101
     */
102
    public function getBaseName ($filePath)
103
    {
104
        return pathinfo($filePath, PATHINFO_FILENAME);
105
    }
106
107
    /**
108
     * Get the name of a given file
109
     *
110
     * @param  string $filePath A file path
111
     *
112
     * @return string
113
     */
114 2
    public function getFileName ($filePath)
115
    {
116 2
        return pathinfo($filePath, PATHINFO_BASENAME);
117
    }
118
119
    /**
120
     * Get the parent directory of a given file
121
     *
122
     * @param  string $filePath A file path
123
     *
124
     * @return string
125
     */
126
    public function getFolderPath ($filePath)
127
    {
128
        return pathinfo($filePath, PATHINFO_DIRNAME);
129
    }
130
131
    /**
132
     * Get the extension of a given file
133
     *
134
     * @param  string $filename A file path
135
     *
136
     * @return string The extension of the file
137
     */
138 23
    public function getExtension ($filename)
139
    {
140 23
        return pathinfo($filename, PATHINFO_EXTENSION);
141
    }
142
143
    /**
144
     * @param string $folderPath
145
     */
146
    public function isDir ($folderPath)
147
    {
148
        return is_dir($folderPath);
149
    }
150
151
    /**
152
     * Get the full path to the file without the extension
153
     *
154
     * @param  string $filename A file path
155
     *
156
     * @return string
157
     */
158
    public function removeExtension ($filename)
159
    {
160
        return $this->appendPath(
161
                   $this->getFolderPath($filename),
162
                   $this->getBaseName($filename)
163
               );
164
    }
165
166
    /**
167
     * Write a file
168
     *
169
     * @param string $targetDir The directory of where the file will be created; the file name is a separate variable
170
     * @param string $fileName  The name of the file
171
     * @param string $content   The content that belongs in the file
172
     *
173
     * @return SplFileInfo A reference to the newly created file
174
     */
175 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...
176
    {
177
        $outputFolder = $this->getFolderPath($this->absolutePath($targetDir, $fileName));
178
        $targetFile   = $this->getFileName($fileName);
179
180
        if (!file_exists($outputFolder))
181
        {
182
            mkdir($outputFolder, 0755, true);
183
        }
184
185
        file_put_contents($this->appendPath($outputFolder, $targetFile), $content, LOCK_EX);
186
187
        return (new SplFileInfo(
188
            $fileName,
189
            $this->absolutePath($targetDir),
190
            $this->absolutePath($targetDir, $fileName))
191
        );
192
    }
193
}