Passed
Branch win (39db04)
by Adam
03:49
created

Symlink::createRelative()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.4882

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 18
ccs 11
cts 16
cp 0.6875
rs 9.2
cc 4
eloc 13
nc 8
nop 3
crap 4.4882
1
<?php
2
3
namespace Genesis\Commands\Filesystem;
4
5
6
use Genesis\Commands\Command;
7
use Genesis\Commands;
8
9
/**
10
 * @author Adam Bisek <[email protected]>
11
 */
12
class Symlink extends Command
13
{
14
15 1
	public function create($target, $link)
16
	{
17 1
		$result = symlink($target, $link);
18 1
		if (!$result) {
19
			$this->error("Cannot create symlink '$target' - '$link'.");
20
		}
21 1
	}
22
23
24
	/**
25
	 * target is relative to link!
26
	 * eg: dir, ../mydir, public/symdir
27
	 */
28 1
	public function createRelative($directory, $target, $link)
29
	{
30 1
		if (!is_dir($directory)) {
31
			$this->error("Directory '$directory' not found.");
32
		}
33 1
		if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
34
			$cmd = 'cd ' . escapeshellarg($directory) . ' && mklink ' . ' /D ' . escapeshellarg($link) . ' ' . escapeshellarg($target);
35
		} else {
36 1
			$cmd = 'cd ' . escapeshellarg($directory) . ' && ln -s ' . escapeshellarg($target) . ' ' . escapeshellarg($link);
37
		}
38 1
		$command = new Commands\Exec();
39 1
		$command->setCommand($cmd);
40 1
		$result = $command->execute();
41 1
		if ($result->getResult() !== 0) {
42
			$this->error("Cannot create symlink '$target' - '$link' in directory '$directory'.");
43 1
		}
44 1
		return $result;
45 1
	}
46
47
}