Completed
Push — master ( f29bd1...c64831 )
by Adam
03:32
created

File::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.4746

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 5
cts 8
cp 0.625
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3.4746
1
<?php
2
3
namespace Genesis\Commands\Filesystem;
4
5
6
use Genesis\Commands\Command;
7
8
/**
9
 * @author Adam Bisek <[email protected]>
10
 */
11
class File extends Command
12
{
13
14 1
	public function create($file, $contents = NULL, $chmod = NULL)
15
	{
16 1
		if (is_file($file)) {
17
			$this->error("File '$file' already exists.");
18
		}
19 1
		$result = file_put_contents($file, $contents);
20 1
		if (!$result) {
21
			$this->error("Cannot create file '$file'.");
22
		}
23 1
		if ($chmod !== NULL) {
24
			$result = chmod($file, $chmod);
25
			if (!$result) {
26
				$this->error("Cannot chmod file '$file'.");
27
			}
28
		}
29 1
	}
30
31
32 4
	public function copy($source, $destination)
33
	{
34 4
		if (!is_file($source)) {
35
			$this->error("Source file '$source' does not exists.");
36
		}
37 4
		if (is_file($destination)) {
38
			$this->error("Destination file '$destination' already exists.");
39
		}
40 4
		$result = copy($source, $destination);
41 4
		if (!$result) {
42
			$this->error("Cannot create file '$destination'.");
43
		}
44 4
	}
45
46
47 1
	public function delete($file)
48
	{
49 1
		if (!is_file($file)) {
50
			return;
51
		}
52 1
		$result = @unlink($file);
53 1
		if(!$result){
54
			$this->error("Cannot delete file '$file'.");
55
		}
56 1
	}
57
58
59 1
	public function makeExecutable($file)
60
	{
61 1
		exec('chmod +x ' . escapeshellarg($file), $output, $result); // native PHP chmod cannot work
62 1
		if ($result !== 0) {
63
			$this->error("Cannot make executable file '$file'.");
64
		}
65 1
	}
66
67
}