Failed Conditions
Push — master ( 305b4a...bd53b9 )
by Alexander
02:57
created

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