Failed Conditions
Push — master ( c1c7d6...c5e998 )
by Alexander
03:04
created

AbstractCommand::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 2
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\Command;
12
13
14
use ConsoleHelpers\ConsoleKit\Command\AbstractCommand as BaseCommand;
15
use ConsoleHelpers\SVNBuddy\Config\AbstractConfigSetting;
16
use ConsoleHelpers\ConsoleKit\Config\ConfigEditor;
17
use ConsoleHelpers\SVNBuddy\Repository\Connector\Connector;
18
use ConsoleHelpers\SVNBuddy\Repository\RevisionLog\RevisionLog;
19
use ConsoleHelpers\SVNBuddy\Repository\RevisionLog\RevisionLogFactory;
20
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
/**
25
 * Base command class.
26
 */
27
abstract class AbstractCommand extends BaseCommand
28
{
29
30
	/**
31
	 * Whatever "path" argument accepts repository urls.
32
	 *
33
	 * @var boolean
34
	 */
35
	protected $pathAcceptsUrl = false;
36
37
	/**
38
	 * Repository connector
39
	 *
40
	 * @var Connector
41
	 */
42
	protected $repositoryConnector;
43
44
	/**
45
	 * Working directory.
46
	 *
47
	 * @var string
48
	 */
49
	protected $workingDirectory = null;
50
51
	/**
52
	 * Revision logs by url
53
	 *
54
	 * @var RevisionLog[]
55
	 */
56
	private $_revisionLogs = array();
57
58
	/**
59
	 * Working copy paths.
60
	 *
61
	 * @var string
62
	 */
63
	private $_workingCopyPaths = array();
64
65
	/**
66
	 * Revision log factory.
67
	 *
68
	 * @var RevisionLogFactory
69
	 */
70
	private $_revisionLogFactory;
71
72
	/**
73
	 * Config editor.
74
	 *
75
	 * @var ConfigEditor
76
	 */
77
	private $_configEditor;
78
79
	/**
80
	 * {@inheritdoc}
81
	 */
82
	protected function initialize(InputInterface $input, OutputInterface $output)
83
	{
84
		$output->getFormatter()->setStyle('debug', new OutputFormatterStyle('white', 'magenta'));
85
86
		parent::initialize($input, $output);
87
	}
88
89
	/**
90
	 * Prepare dependencies.
91
	 *
92
	 * @return void
93
	 */
94
	protected function prepareDependencies()
95
	{
96
		parent::prepareDependencies();
97
98
		$container = $this->getContainer();
99
100
		$this->repositoryConnector = $container['repository_connector'];
101
		$this->_revisionLogFactory = $container['revision_log_factory'];
102
		$this->workingDirectory = $container['working_directory'];
103
		$this->_configEditor = $container['config_editor'];
104
	}
105
106
	/**
107
	 * Returns command setting value.
108
	 *
109
	 * @param string      $name         Name.
110
	 * @param string|null $command_name Command name to get settings from instead of current command.
111
	 *
112
	 * @return mixed
113
	 */
114
	protected function getSetting($name, $command_name = null)
115
	{
116
		return $this->_getConfigSetting($name, $command_name)->getValue();
117
	}
118
119
	/**
120
	 * Sets command setting value.
121
	 *
122
	 * @param string      $name         Name.
123
	 * @param mixed       $value        Value.
124
	 * @param string|null $command_name Command name to get settings from instead of current command.
125
	 *
126
	 * @return void
127
	 */
128
	protected function setSetting($name, $value, $command_name = null)
129
	{
130
		$this->_getConfigSetting($name, $command_name)->setValue($value);
131
	}
132
133
	/**
134
	 * Validates command setting usage.
135
	 *
136
	 * @param string      $name         Name.
137
	 * @param string|null $command_name Command name to get settings from instead of current command.
138
	 *
139
	 * @return AbstractConfigSetting
140
	 * @throws \LogicException When command don't have any config settings to provide.
141
	 * @throws \LogicException When config setting is not found.
142
	 */
143
	private function _getConfigSetting($name, $command_name = null)
144
	{
145
		// By default access own config settings.
146
		if ( !isset($command_name) ) {
147
			$command_name = $this->getName();
148
		}
149
150
		/** @var IConfigAwareCommand $command */
151
		$command = $this->getApplication()->get($command_name);
152
153
		if ( !($command instanceof IConfigAwareCommand) ) {
154
			throw new \LogicException('The "' . $command_name . '" command does not have any settings.');
155
		}
156
157
		foreach ( $command->getConfigSettings() as $config_setting ) {
158
			if ( $config_setting->getName() === $name ) {
159
				if ( $config_setting->isWithinScope(AbstractConfigSetting::SCOPE_WORKING_COPY) ) {
160
					$config_setting->setWorkingCopyUrl($this->getWorkingCopyUrl());
161
				}
162
163
				$config_setting->setEditor($this->_configEditor);
164
165
				return $config_setting;
166
			}
167
		}
168
169
		throw new \LogicException('The "' . $command_name . '" command doesn\'t have "' . $name . '" config setting.');
170
	}
171
172
	/**
173
	 * Prepare setting prefix.
174
	 *
175
	 * @param boolean $is_global Return global setting prefix.
176
	 *
177
	 * @return string
178
	 */
179
	protected function getConfigScope($is_global)
180
	{
181
		if ( $is_global ) {
182
			return 'global-settings.';
183
		}
184
185
		return 'path-settings[' . $this->getWorkingCopyUrl() . '].';
186
	}
187
188
	/**
189
	 * Returns revision log.
190
	 *
191
	 * @param string $repository_url Repository url.
192
	 *
193
	 * @return RevisionLog
194
	 */
195
	protected function getRevisionLog($repository_url)
196
	{
197
		if ( !isset($this->_revisionLogs[$repository_url]) ) {
198
			$this->_revisionLogs[$repository_url] = $this->_revisionLogFactory->getRevisionLog(
199
				$repository_url,
200
				$this->io
201
			);
202
		}
203
204
		return $this->_revisionLogs[$repository_url];
205
	}
206
207
	/**
208
	 * Transforms string into list.
209
	 *
210
	 * @param string $string    String.
211
	 * @param string $separator Separator.
212
	 *
213
	 * @return array
214
	 */
215
	protected function getList($string, $separator = ',')
216
	{
217
		return array_filter(array_map('trim', explode($separator, $string)));
218
	}
219
220
	/**
221
	 * Returns URL to the working copy.
222
	 *
223
	 * @return string
224
	 */
225
	protected function getWorkingCopyUrl()
226
	{
227
		return $this->repositoryConnector->getWorkingCopyUrl($this->getWorkingCopyPath());
228
	}
229
230
	/**
231
	 * Return working copy path.
232
	 *
233
	 * @return string
234
	 * @throws \RuntimeException When folder isn't a working copy.
235
	 */
236
	protected function getWorkingCopyPath()
237
	{
238
		$path = $this->getPath();
239
240
		if ( !in_array($path, $this->_workingCopyPaths) ) {
241
			if ( !$this->repositoryConnector->isUrl($path)
242
				&& !$this->repositoryConnector->isWorkingCopy($path)
243
			) {
244
				throw new \RuntimeException('The "' . $path . '" isn\'t a working copy.');
245
			}
246
247
			$this->_workingCopyPaths[] = $path;
248
		}
249
250
		return $path;
251
	}
252
253
	/**
254
	 * Returns all refs.
255
	 *
256
	 * @return array
257
	 */
258
	protected function getAllRefs()
259
	{
260
		$wc_url = $this->getWorkingCopyUrl();
261
		$revision_log = $this->getRevisionLog($wc_url);
262
263
		return $revision_log->find('refs', 'all_refs');
264
	}
265
266
	/**
267
	 * Return working copy path.
268
	 *
269
	 * @return string
270
	 * @throws \RuntimeException When url was given instead of path.
271
	 */
272
	protected function getPath()
273
	{
274
		// During auto-complete the IO isn't set.
275
		if ( !isset($this->io) ) {
276
			$path = '.';
277
		}
278
		else {
279
			$path = $this->io->getArgument('path');
280
		}
281
282
		if ( !$this->repositoryConnector->isUrl($path) ) {
283
			$path = realpath($path);
284
		}
285
		else {
286
			if ( !$this->pathAcceptsUrl ) {
287
				throw new \RuntimeException('The "path" argument must be a working copy path and not URL.');
288
			}
289
290
			$path = $this->repositoryConnector->removeCredentials($path);
291
		}
292
293
		return $path;
294
	}
295
296
}
297