File   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 0
dl 0
loc 93
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createFile() 0 5 3
A readFile() 0 9 2
A renderFile() 0 10 2
A writeFile() 0 5 2
A removeFile() 0 5 2
1
<?php
2
namespace Redaxscript\Filesystem;
3
4
use function chmod;
5
use function file_get_contents;
6
use function file_put_contents;
7
use function is_file;
8
use function ob_get_clean;
9
use function ob_start;
10
use function strlen;
11
use function touch;
12
use function unlink;
13
14
/**
15
 * children class to handle a file in the filesystem
16
 *
17
 * @since 3.2.0
18
 *
19
 * @package Redaxscript
20
 * @category Filesystem
21
 * @author Henry Ruhs
22
 */
23
24
class File extends Filesystem
25
{
26
	/**
27
	 * create the file
28
	 *
29
	 * @since 3.2.0
30
	 *
31
	 * @param string $file name of the file
32
	 * @param int $mode file access mode
33
	 *
34
	 * @return bool
35
	 */
36
37 2
	public function createFile(string $file = null, int $mode = 0777) : bool
38
	{
39 2
		$path = $this->_root . DIRECTORY_SEPARATOR . $file;
40 2
		return !is_file($path) && touch($path) && chmod($path, $mode);
41
	}
42
43
	/**
44
	 * read content of file
45
	 *
46
	 * @since 3.0.0
47
	 *
48
	 * @param string $file name of the file
49
	 *
50
	 * @return string|null
51
	 */
52
53 2
	public function readFile(string $file = null) : ?string
54
	{
55 2
		$path = $this->_root . DIRECTORY_SEPARATOR . $file;
56 2
		if (is_file($path))
57
		{
58 2
			return file_get_contents($path);
59
		}
60
		return null;
61
	}
62
63
	/**
64
	 * render content of file
65
	 *
66
	 * @since 3.0.0
67
	 *
68
	 * @param string $file name of the file
69
	 *
70
	 * @return string
71
	 */
72
73 2
	public function renderFile(string $file = null) : string
74
	{
75 2
		$path = $this->_root . DIRECTORY_SEPARATOR . $file;
76 2
		ob_start();
77 2
		if (is_file($path))
78
		{
79 2
			include($path);
80
		}
81 2
		return ob_get_clean();
82
	}
83
84
	/**
85
	 * write content to file
86
	 *
87
	 * @since 3.0.0
88
	 *
89
	 * @param string $file name of the file
90
	 * @param string $content content of the file
91
	 *
92
	 * @return bool
93
	 */
94
95 2
	public function writeFile(string $file = null, string $content = null) : bool
96
	{
97 2
		$path = $this->_root . DIRECTORY_SEPARATOR . $file;
98 2
		return strlen($content) && file_put_contents($path, $content) > 0;
99
	}
100
101
	/**
102
	 * remove the file
103
	 *
104
	 * @since 3.2.0
105
	 *
106
	 * @param string $file name of the file
107
	 *
108
	 * @return bool
109
	 */
110
111 3
	public function removeFile(string $file = null) : bool
112
	{
113 3
		$path = $this->_root . DIRECTORY_SEPARATOR . $file;
114 3
		return is_file($path) && unlink($path);
115
	}
116
}
117