CommandFactory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 34
c 1
b 0
f 0
dl 0
loc 129
ccs 33
cts 33
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getCommand() 0 9 1
A prepareSvnCommand() 0 15 3
A buildCommand() 0 17 4
1
<?php
2
/**
3
 * This file is part of the SVN-Buddy library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/svn-buddy
9
 */
10
11
namespace ConsoleHelpers\SVNBuddy\Repository\Connector;
12
13
14
use ConsoleHelpers\ConsoleKit\Config\ConfigEditor;
15
use ConsoleHelpers\ConsoleKit\ConsoleIO;
16
use ConsoleHelpers\SVNBuddy\Cache\CacheManager;
17
use ConsoleHelpers\SVNBuddy\Process\IProcessFactory;
18
19
class CommandFactory
20
{
21
22
	/**
23
	 * Reference to configuration.
24
	 *
25
	 * @var ConfigEditor
26
	 */
27
	private $_configEditor;
28
29
	/**
30
	 * Process factory.
31
	 *
32
	 * @var IProcessFactory
33
	 */
34
	private $_processFactory;
35
36
	/**
37
	 * Console IO.
38
	 *
39
	 * @var ConsoleIO
40
	 */
41
	private $_io;
42
43
	/**
44
	 * Cache manager.
45
	 *
46
	 * @var CacheManager
47
	 */
48
	private $_cacheManager;
49
50
	/**
51
	 * Path to a svn command.
52
	 *
53
	 * @var array
54
	 */
55
	private $_svnCommand = array('svn');
56
57
	/**
58
	 * Creates command factory.
59
	 *
60
	 * @param ConfigEditor    $config_editor   ConfigEditor.
61
	 * @param IProcessFactory $process_factory Process factory.
62
	 * @param ConsoleIO       $io              Console IO.
63
	 * @param CacheManager    $cache_manager   Cache manager.
64
	 */
65 19
	public function __construct(
66
		ConfigEditor $config_editor,
67
		IProcessFactory $process_factory,
68
		ConsoleIO $io,
69
		CacheManager $cache_manager
70
	) {
71 19
		$this->_configEditor = $config_editor;
72 19
		$this->_processFactory = $process_factory;
73 19
		$this->_io = $io;
74 19
		$this->_cacheManager = $cache_manager;
75
76 19
		$this->prepareSvnCommand();
77
	}
78
79
	/**
80
	 * Prepares static part of svn command to be used across the script.
81
	 *
82
	 * @return void
83
	 */
84 19
	protected function prepareSvnCommand()
85
	{
86 19
		$username = $this->_configEditor->get('repository-connector.username');
87 19
		$password = $this->_configEditor->get('repository-connector.password');
88
89 19
		$this->_svnCommand[] = '--non-interactive';
90
91 19
		if ( $username ) {
92 2
			$this->_svnCommand[] = '--username';
93 2
			$this->_svnCommand[] = $username;
94
		}
95
96 19
		if ( $password ) {
97 2
			$this->_svnCommand[] = '--password';
98 2
			$this->_svnCommand[] = $password;
99
		}
100
	}
101
102
	/**
103
	 * Builds a command.
104
	 *
105
	 * @param string $sub_command Sub command.
106
	 * @param array  $arguments   Arguments.
107
	 *
108
	 * @return Command
109
	 */
110 10
	public function getCommand($sub_command, array $arguments = array())
111
	{
112 10
		$command_line = $this->buildCommand($sub_command, $arguments);
113
114 9
		return new Command(
115 9
			$command_line,
116 9
			$this->_io,
117 9
			$this->_cacheManager,
118 9
			$this->_processFactory
119 9
		);
120
	}
121
122
	/**
123
	 * Builds command from given arguments.
124
	 *
125
	 * @param string $sub_command Command.
126
	 * @param array  $arguments   Arguments.
127
	 *
128
	 * @return array
129
	 * @throws \InvalidArgumentException When command contains spaces.
130
	 */
131 10
	protected function buildCommand($sub_command, array $arguments = array())
132
	{
133 10
		if ( strpos($sub_command, ' ') !== false ) {
134 1
			throw new \InvalidArgumentException('The "' . $sub_command . '" sub-command contains spaces.');
135
		}
136
137 9
		$command_line = $this->_svnCommand;
138
139 9
		if ( !empty($sub_command) ) {
140 4
			$command_line[] = $sub_command;
141
		}
142
143 9
		if ( !empty($arguments) ) {
144 8
			$command_line = array_merge($command_line, $arguments);
145
		}
146
147 9
		return $command_line;
148
	}
149
150
}
151