Completed
Push — master ( aff38e...dfd432 )
by Alexander
03:13
created

AbstractCommand::getSetting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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