Completed
Pull Request — master (#91)
by Vladimir
05:42
created

Folder::freeze()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 0
crap 2
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 12
    /**
25
     * @param string $folderPath
26 12
     */
27
    public function __construct($folderPath)
28 12
    {
29
        $this->frozen = false;
30
        $this->folder = new FilesystemPath($folderPath);
31
32 12
        if (!$this->folder->isDir())
33
        {
34
            throw new FileNotFoundException(sprintf('The folder could not be found: %s', $folderPath));
35
        }
36
    }
37
38
    public function __toString()
39
    {
40
        return (string)$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
    public function getPath()
49
    {
50
        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
    public function freeze()
57
    {
58
        $this->frozen = true;
59
    }
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 Support for freezing was added
77
     * @since 0.1.0
78
     *
79
     * @throws \Exception
80
     */
81 12
    public function setTargetDirectory($folderName)
82
    {
83 12
        if ($this->isFrozen())
84 12
        {
85
            throw new \Exception('A frozen folder object cannot be modified.');
86 12
        }
87
88 9
        if ($folderName === null || empty($folderName))
89
        {
90
            return;
91 12
        }
92
93 12
        $this->folder->appendToPath($folderName);
94
    }
95
96
    /**
97
     * Copy a file from an absolute file to a path relative to the Folder's location.
98
     *
99
     * @param string $absolutePath The absolute path of the file
100
     * @param string $targetPath   The relative file path to the Folder's location
101
     *
102
     * @since 0.1.0
103
     */
104
    public function copyFile($absolutePath, $targetPath)
105
    {
106
        $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...
107
108
        fs::copy($absolutePath, $targetPath, true);
109
    }
110
111
    /**
112
     * Write a file with the specified content.
113
     *
114
     * @param string $relativePath The file path relative to this Folder's location
115
     * @param string $content      The content that will be written to the file
116
     *
117
     * @since 0.1.0
118
     *
119
     * @return File
120
     */
121
    public function writeFile($relativePath, $content)
122
    {
123
        $targetFile = $this->folder->generatePath($relativePath);
124
        $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...
125
126
        if (!file_exists($targetFolderPath))
127
        {
128
            mkdir($targetFolderPath, 0755, true);
129
        }
130
131
        file_put_contents($targetFile, $content);
132
133
        return new File($targetFile);
134
    }
135
}
136