|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LazyEight\DiTesto\FileSystem; |
|
4
|
|
|
|
|
5
|
|
|
use LazyEight\DiTesto\Interfaces\FileSystem\FileSystemPathInterface; |
|
6
|
|
|
use LazyEight\DiTesto\Interfaces\FileSystem\FileSystemWriterInterface; |
|
7
|
|
|
use LazyEight\DiTesto\FileSystem\Exceptions\FileSystemException; |
|
8
|
|
|
use LazyEight\DiTesto\FileSystem\Exceptions\InvalidPathException; |
|
9
|
|
|
|
|
10
|
|
|
class FileSystemWriter implements FileSystemWriterInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var FileSystemPathInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
private $path; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* FileSystemWriter constructor. |
|
19
|
|
|
* @param FileSystemPathInterface $path |
|
20
|
|
|
*/ |
|
21
|
2 |
|
public function __construct(FileSystemPathInterface $path) |
|
22
|
|
|
{ |
|
23
|
2 |
|
$this->path = $path; |
|
24
|
2 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @inheritDoc |
|
28
|
|
|
*/ |
|
29
|
1 |
|
public function isWritable(): bool |
|
30
|
|
|
{ |
|
31
|
1 |
|
return is_writable($this->path->rawPath()); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @inheritDoc |
|
36
|
|
|
*/ |
|
37
|
1 |
|
public function write(string $content) |
|
38
|
|
|
{ |
|
39
|
1 |
|
$this->validate(); |
|
40
|
1 |
|
file_put_contents($this->path->rawPath(), $content); |
|
41
|
1 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
1 |
|
private function isWritablePath() |
|
47
|
|
|
{ |
|
48
|
1 |
|
return is_writable($this->path->pathName()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @throws FileSystemException |
|
53
|
|
|
* @throws InvalidPathException |
|
54
|
|
|
*/ |
|
55
|
4 |
|
private function validate() |
|
56
|
|
|
{ |
|
57
|
4 |
|
$this->validateIsDirectory(); |
|
58
|
3 |
|
$this->validatePathIsWritable(); |
|
59
|
2 |
|
$this->validateWritableFile(); |
|
60
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @throws InvalidPathException |
|
64
|
|
|
*/ |
|
65
|
3 |
|
private function validateIsDirectory() |
|
66
|
|
|
{ |
|
67
|
3 |
|
if ($this->path->isDirectory()) { |
|
68
|
1 |
|
throw new InvalidPathException("Error, can't write file content to a directory."); |
|
69
|
|
|
} |
|
70
|
2 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @throws FileSystemException |
|
74
|
|
|
*/ |
|
75
|
2 |
|
private function validatePathIsWritable() |
|
76
|
|
|
{ |
|
77
|
2 |
|
if (!file_exists($this->path->rawPath()) && !$this->isWritablePath()) { |
|
78
|
1 |
|
throw new FileSystemException("Error, can't write to the file. The path must be writable."); |
|
79
|
|
|
} |
|
80
|
1 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @throws FileSystemException |
|
84
|
|
|
*/ |
|
85
|
2 |
|
private function validateWritableFile() |
|
86
|
|
|
{ |
|
87
|
2 |
|
if (file_exists($this->path->rawPath()) && !$this->isWritable()) { |
|
88
|
1 |
|
throw new FileSystemException("Error, can't write to the file. The file must be writable."); |
|
89
|
|
|
} |
|
90
|
1 |
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|