Completed
Pull Request — master (#78)
by Vladimir
02:14
created

Folder::writeFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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