Completed
Push — master ( 5bbfc4...6ddfd6 )
by Vladimir
02:33
created

Folder::getFilesystemPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Filesystem;
9
10
use allejo\stakx\Filesystem\FilesystemLoader as fs;
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 bool */
19
    private $frozen;
20
21
    /** @var FilesystemPath */
22
    private $folder;
23
24
    /**
25
     * @param string $folderPath
26
     */
27 13
    public function __construct($folderPath)
28
    {
29 13
        $this->frozen = false;
30 13
        $this->folder = new FilesystemPath($folderPath);
31
32 13
        if (!$this->folder->isDir())
33
        {
34
            throw new FileNotFoundException(sprintf('The folder could not be found: %s', $folderPath));
35
        }
36 13
    }
37
38 1
    public function __toString()
39
    {
40 1
        return $this->folder->getAbsolutePath();
41
    }
42
43
    /**
44
     * Get the file path to this Folder in an OOP friendly way.
45
     *
46
     * @return FilesystemPath
47
     */
48 1
    public function getFilesystemPath()
49
    {
50 1
        return new FilesystemPath($this->__toString());
51
    }
52
53
    /**
54
     * Set this Folder to a "frozen" state meaning its path can no longer be modified.
55
     */
56 1
    public function freeze()
57
    {
58 1
        $this->frozen = true;
59 1
    }
60
61
    /**
62
     * Check whether or not this Folder's path has been frozen.
63
     *
64
     * @return bool
65
     */
66
    public function isFrozen()
67
    {
68
        return $this->frozen;
69
    }
70
71
    /**
72
     * Set a base folder that will be prefixed before all file writes and copies.
73
     *
74
     * @param string $folderName
75
     *
76
     * @since 0.2.0 An \Exception is thrown when a frozen Folder is attempted to
77
     *              be modified
78
     * @since 0.1.0
79
     *
80
     * @throws \Exception
81
     */
82
    public function setTargetDirectory($folderName)
83
    {
84
        if ($this->isFrozen())
85
        {
86
            throw new \Exception('A frozen folder object cannot be modified.');
87
        }
88
89
        if ($folderName === null || empty($folderName))
90
        {
91
            return;
92
        }
93
94
        $this->folder->appendToPath($folderName);
95
    }
96
97
    /**
98
     * Copy a file from an absolute file to a path relative to the Folder's location.
99
     *
100
     * @param string $absolutePath The absolute path of the file
101
     * @param string $targetPath   The relative file path to the Folder's location
102
     *
103
     * @since 0.1.0
104
     */
105
    public function copyFile($absolutePath, $targetPath)
106
    {
107
        $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...
108
109
        fs::copy($absolutePath, $targetPath, true);
110
    }
111
112
    /**
113
     * Write a file with the specified content.
114
     *
115
     * @param string $relativePath The file path relative to this Folder's location
116
     * @param string $content      The content that will be written to the file
117
     *
118
     * @since 0.1.0
119
     *
120
     * @return File
121
     */
122 12
    public function writeFile($relativePath, $content)
123
    {
124 12
        $targetFile = $this->folder->generatePath($relativePath);
125 12
        $targetFolderPath = fs::getFolderPath($targetFile);
0 ignored issues
show
Documentation introduced by
$targetFile is of type string, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
126
127 12
        if (!file_exists($targetFolderPath))
128
        {
129 9
            mkdir($targetFolderPath, 0755, true);
130
        }
131
132 12
        file_put_contents($targetFile, $content);
133
134 12
        return new File($targetFile);
135
    }
136
}
137