Completed
Push — master ( 3df90d...74371a )
by Vladimir
04:10
created

Folder::copyFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
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\Filesystem;
9
10
use allejo\stakx\System\Filesystem;
11
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
12
13
/**
14
 * A representation of a folder on a given filesystem.
15
 */
16
final class Folder
17
{
18
    /** @var Filesystem */
19
    private $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...
20
    /** @var FilesystemPath */
21
    private $folder;
22
23
    /**
24
     * @param string $folderPath
25
     */
26 12
    public function __construct($folderPath)
27
    {
28 12
        $this->fs = new Filesystem();
29 12
        $this->folder = new FilesystemPath($folderPath);
30
31 12
        if (!$this->folder->isDir())
32 12
        {
33
            throw new FileNotFoundException(sprintf('The folder could not be found: %s', $folderPath));
34
        }
35 12
    }
36
37
    public function __toString()
38
    {
39
        return $this->folder->getAbsolutePath();
40
    }
41
42
    /**
43
     * Set a base folder that will be prefixed before all file writes and copies.
44
     *
45
     * @param string $folderName
46
     *
47
     * @since 0.1.0
48
     */
49
    public function setTargetDirectory($folderName)
50
    {
51
        if ($folderName === null || empty($folderName))
52
        {
53
            return;
54
        }
55
56
        $this->folder->appendToPath($folderName);
57
    }
58
59
    /**
60
     * Copy a file from an absolute file to a path relative to the Folder's location.
61
     *
62
     * @param string $absolutePath The absolute path of the file
63
     * @param string $targetPath   The relative file path to the Folder's location
64
     *
65
     * @since 0.1.0
66
     */
67
    public function copyFile($absolutePath, $targetPath)
68
    {
69
        $targetPath = $this->folder->generatePath($targetPath);
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...
70
71
        $this->fs->copy($absolutePath, $targetPath, true);
72
    }
73
74
    /**
75
     * Write a file with the specified content.
76
     *
77
     * @param string $relativePath The file path relative to this Folder's location
78
     * @param string $content      The content that will be written to the file
79
     *
80
     * @since 0.1.0
81
     *
82
     * @return File
83
     */
84 12
    public function writeFile($relativePath, $content)
85
    {
86 12
        $targetFile = $this->folder->generatePath($relativePath);
87 12
        $targetFolderPath = $this->fs->getFolderPath($targetFile);
88
89 12
        if (!file_exists($targetFolderPath))
90 12
        {
91 9
            mkdir($targetFolderPath, 0755, true);
92 9
        }
93
94 12
        file_put_contents($targetFile, $content);
95
96 12
        return (new File($targetFile));
97
    }
98
}
99