Completed
Push — master ( 937b8a...f9317c )
by Vladimir
02:35
created

Folder::buildPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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\Finder\SplFileInfo;
12
13
/**
14
 * All folder paths stored inside of this class will **not** have the ending DIRECTORY_SEPARATOR.
15
 *
16
 * @internal
17
 */
18
class Folder
19
{
20
    protected $fs;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $fs. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
21
    protected $absolutePath;
22
    protected $targetDirectories;
23
24
    /**
25
     * @param string $folderPath
26
     */
27 36
    public function __construct($folderPath)
28
    {
29 36
        $this->fs = new Filesystem();
30 36
        $this->targetDirectories = array();
31
32
        // Setup the absolute path to the directory
33 36
        if (substr($folderPath, 0, 1) === DIRECTORY_SEPARATOR)
34 36
        {
35
            $this->absolutePath = $folderPath;
36
        }
37
        else
38
        {
39 36
            $this->absolutePath = $this->fs->absolutePath($folderPath);
40
        }
41
42
        // Ensure our directory paths will don't end with a '/'
43 36
        $this->absolutePath = rtrim($this->absolutePath, DIRECTORY_SEPARATOR);
44
45 36
        if (!$this->fs->isDir($this->absolutePath))
46 36
        {
47
            throw new \InvalidArgumentException();
48
        }
49
50 36
        if (!$this->fs->exists($this->absolutePath))
51 36
        {
52
            throw new FileNotFoundException();
53
        }
54 36
    }
55
56
    public function __toString()
57
    {
58
        return rtrim($this->absolutePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
59
    }
60
61
    /**
62
     * Set a base folder that will be prefixed before all file writes and copies.
63
     *
64
     * @param string $folderName
65
     *
66
     * @since 0.1.0
67
     */
68
    public function setTargetDirectory($folderName)
69
    {
70
        if (is_null($folderName) || empty($folderName))
71
        {
72
            $this->targetDirectories = array();
73
        }
74
        else
75
        {
76
            $this->targetDirectories[] = trim($folderName, DIRECTORY_SEPARATOR);
77
        }
78
    }
79
80
    /**
81
     * Copy a file from an absolute file to a path relative to the Folder's location.
82
     *
83
     * @param string $absolutePath The absolute path of the file
84
     * @param string $targetPath   The relative file path to the Folder's location
85
     *
86
     * @since 0.1.0
87
     */
88
    public function copyFile($absolutePath, $targetPath)
89
    {
90
        $targetPath = ltrim($targetPath, DIRECTORY_SEPARATOR);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $targetPath. This often makes code more readable.
Loading history...
91
92
        $this->fs->copy(
93
            $absolutePath,
94
            $this->buildPath($this->getCwd(), $targetPath),
95
            true
96
        );
97
    }
98
99
    /**
100
     * Write a file with the specified content.
101
     *
102
     * @param string $relativePath The file path relative to this Folder's location
103
     * @param string $content      The content that will be written to the file
104
     *
105
     * @since 0.1.0
106
     *
107
     * @return SplFileInfo
108
     */
109 36
    public function writeFile($relativePath, $content)
110
    {
111 36
        $outputFolder = $this->fs->getFolderPath($relativePath);
112 36
        $targetFileName = $this->fs->getFileName($outputFolder);
113
114 36
        $absoluteFolderPath = $this->buildPath($this->getCwd(), $outputFolder);
115
116 36
        if (!file_exists($absoluteFolderPath))
117 36
        {
118 12
            mkdir($absoluteFolderPath, 0755, true);
119 12
        }
120
121 36
        file_put_contents(
122 36
            $this->buildPath($this->getCwd(), $relativePath),
123
            $content
124 36
        );
125
126 36
        return (new SplFileInfo(
127 36
            $targetFileName,
128 36
            $absoluteFolderPath,
129 36
            $this->buildPath($absoluteFolderPath, $targetFileName)
130 36
        ));
131
    }
132
133
    /**
134
     * @param string $pathFragments
135
     *
136
     * @return string
137
     */
138 36
    private function buildPath($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...
139
    {
140 36
        $paths = func_get_args();
141
142 36
        return implode(DIRECTORY_SEPARATOR, $paths);
143
    }
144
145
    /**
146
     * Returns the absolute path of where files will be placed.
147
     *
148
     * @return string
149
     */
150 36
    private function getCwd()
151
    {
152 36
        $location = array_merge(array($this->absolutePath), $this->targetDirectories);
153
154 36
        return implode(DIRECTORY_SEPARATOR, $location);
155
    }
156
}
157