Completed
Push — master ( 9b09f6...8fd86e )
by Adam
03:29
created

Git::setCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
4
namespace Genesis\Commands;
5
6
7
use Genesis\InvalidArgumentException;
8
9
10
/**
11
 * @author Adam Bisek <[email protected]>
12
 */
13
class Git extends Command
14
{
15
16
	/** @var string */
17
	private $gitExecutable = 'git';
18
19
	private $command;
20
21
	private $workingDirectory;
22
23
	/**
24
	 * Redirect stderr to stdout? - for testing purposes only
25
	 * Git writes his progressive output do stderr (even if everything ok) - at least when clonning
26
	 * @var bool
27
	 */
28
	private $redirectStderrToStdout = FALSE;
29
30
31
	/**
32
	 * Returns path to git executable
33
	 * @return string
34
	 */
35 1
	public function getGitExecutable()
36
	{
37 1
		return $this->gitExecutable;
38
	}
39
40
41
	/**
42
	 * Sets path to git executable
43
	 * @param string $gitExecutable
44
	 */
45 2
	public function setGitExecutable($gitExecutable)
46
	{
47 2
		if ($gitExecutable == '') {
48 1
			throw new InvalidArgumentException("Git executable cannot be empty.");
49
		}
50 1
		$this->gitExecutable = $gitExecutable;
51 1
	}
52
53
54
	/**
55
	 * @return string
56
	 */
57 1
	public function getWorkingDirectory()
58
	{
59 1
		return $this->workingDirectory;
60
	}
61
62
63
	/**
64
	 * Sets path to working directory
65
	 * @param string $workingTree
0 ignored issues
show
Bug introduced by
There is no parameter named $workingTree. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
66
	 */
67 2
	public function setWorkingDirectory($workingDirectory)
68
	{
69 2
		$this->workingDirectory = $workingDirectory;
70 2
	}
71
72
73
	/**
74
	 * @return boolean
75
	 */
76 1
	public function isRedirectStderrToStdout()
77
	{
78 1
		return $this->redirectStderrToStdout;
79
	}
80
81
82
	/**
83
	 * @param boolean $redirectStderrToStdout
84
	 */
85 2
	public function setRedirectStderrToStdout($redirectStderrToStdout)
86
	{
87 2
		$this->redirectStderrToStdout = $redirectStderrToStdout;
88 2
	}
89
90
91
	/**
92
	 * Return setted git command
93
	 * @return mixed
94
	 */
95 1
	public function getCommand()
96
	{
97 1
		return $this->command;
98
	}
99
100
101
	/**
102
	 * Sets git command (to be executed later)
103
	 * @param mixed $command
104
	 */
105 3
	public function setCommand($command)
106
	{
107 3
		$this->command = $command;
108 3
	}
109
110
111
	/**
112
	 * Sets git command for cloning repository (to be executed later)
113
	 */
114 1
	public function cloneRepo($url, $branch = NULL, $dir = NULL)
115
	{
116 1
		$command = "clone";
117 1
		$command .= " --depth 1 --recursive $url";
118 1
		$command .= ($branch ? " --branch $branch" : '');
119 1
		$command .= ($dir ? ' ' . escapeshellarg($dir) : '');
120 1
		$this->setCommand($command);
121 1
	}
122
123
124
	/**
125
	 * @return ExecResult
126
	 */
127 2
	public function execute()
128
	{
129 2
		$command = '';
130 2
		if($this->workingDirectory !== NULL){
131 1
			$command .= 'cd ' . escapeshellarg($this->workingDirectory) . ' && ';
132 1
		}
133 2
		$command .= escapeshellarg($this->gitExecutable);
134 2
		$execCommand = $command . ' ' . $this->command;
135 2
		if($this->redirectStderrToStdout){
136 1
			$execCommand .= ' 2>&1';
137 1
		}
138 2
		return $this->exec($execCommand);
139
	}
140
141
142 2
	private function exec($command)
143
	{
144 2
		$exec = new Exec();
145 2
		$exec->setCommand($command);
146 2
		return $exec->execute();
147
	}
148
149
}