Failed Conditions
Branch master (acad7e)
by Alexander
18:14 queued 08:15
created

AbstractCommand::getAllRefs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

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 7
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
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\CommandConfig;
16
use ConsoleHelpers\SVNBuddy\Repository\Connector\Connector;
17
use ConsoleHelpers\SVNBuddy\Repository\RevisionLog\RevisionLog;
18
use ConsoleHelpers\SVNBuddy\Repository\RevisionLog\RevisionLogFactory;
19
use ConsoleHelpers\SVNBuddy\Repository\WorkingCopyResolver;
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
	 * Raw path.
32
	 *
33
	 * @var string
34
	 */
35
	private $_rawPath;
36
37
	/**
38
	 * Whatever "path" argument accepts repository urls.
39
	 *
40
	 * @var boolean
41
	 */
42
	protected $pathAcceptsUrl = false;
43
44
	/**
45
	 * Repository connector
46
	 *
47
	 * @var Connector
48
	 */
49
	protected $repositoryConnector;
50
51
	/**
52
	 * Working directory.
53
	 *
54
	 * @var string
55
	 */
56
	protected $workingDirectory = null;
57
58
	/**
59
	 * Working copy resolver.
60
	 *
61
	 * @var WorkingCopyResolver
62
	 */
63
	private $_workingCopyResolver = null;
64
65
	/**
66
	 * Revision log factory.
67
	 *
68
	 * @var RevisionLogFactory
69
	 */
70
	private $_revisionLogFactory;
71
72
	/**
73
	 * Command config.
74
	 *
75
	 * @var CommandConfig
76
	 */
77
	private $_commandConfig;
78
79
	/**
80
	 * {@inheritdoc}
81
	 *
82
	 * @throws \RuntimeException When url was given instead of path.
83
	 */
84
	protected function initialize(InputInterface $input, OutputInterface $output)
85
	{
86
		$this->_rawPath = null;
87
		$output->getFormatter()->setStyle('debug', new OutputFormatterStyle('white', 'magenta'));
88
89
		parent::initialize($input, $output);
90
91
		// Only apply check for commands, that accept working copy path.
92
		if ( $input->hasArgument('path') ) {
93
			if ( !$this->pathAcceptsUrl && $this->repositoryConnector->isUrl($this->getRawPath()) ) {
94
				throw new \RuntimeException('The "path" argument must be a working copy path and not URL.');
95
			}
96
		}
97
	}
98
99
	/**
100
	 * Prepare dependencies.
101
	 *
102
	 * @return void
103
	 */
104
	protected function prepareDependencies()
105
	{
106
		parent::prepareDependencies();
107
108
		$container = $this->getContainer();
109
110
		$this->_workingCopyResolver = $container['working_copy_resolver'];
111
		$this->repositoryConnector = $container['repository_connector'];
112
		$this->_revisionLogFactory = $container['revision_log_factory'];
113
		$this->workingDirectory = $container['working_directory'];
114
		$this->_commandConfig = $container['command_config'];
115
	}
116
117
	/**
118
	 * Returns command setting value.
119
	 *
120
	 * @param string $name Name.
121
	 *
122
	 * @return mixed
123
	 */
124
	protected function getSetting($name)
125
	{
126
		return $this->_commandConfig->getSettingValue($name, $this, $this->getRawPath());
127
	}
128
129
	/**
130
	 * Sets command setting value.
131
	 *
132
	 * @param string $name  Name.
133
	 * @param mixed  $value Value.
134
	 *
135
	 * @return void
136
	 */
137
	protected function setSetting($name, $value)
138
	{
139
		$this->_commandConfig->setSettingValue($name, $this, $this->getRawPath(), $value);
140
	}
141
142
	/**
143
	 * Returns revision log.
144
	 *
145
	 * @param string $repository_url Repository url.
146
	 *
147
	 * @return RevisionLog
148
	 */
149
	protected function getRevisionLog($repository_url)
150
	{
151
		return $this->_revisionLogFactory->getRevisionLog($repository_url, $this->io);
152
	}
153
154
	/**
155
	 * Transforms string into list.
156
	 *
157
	 * @param string $string    String.
158
	 * @param string $separator Separator.
159
	 *
160
	 * @return array
161
	 */
162
	protected function getList($string, $separator = ',')
163
	{
164
		return array_filter(array_map('trim', explode($separator, $string)));
165
	}
166
167
	/**
168
	 * Returns URL to the working copy.
169
	 *
170
	 * @return string
171
	 */
172
	protected function getWorkingCopyUrl()
173
	{
174
		return $this->_workingCopyResolver->getWorkingCopyUrl($this->getRawPath());
175
	}
176
177
	/**
178
	 * Return working copy path.
179
	 *
180
	 * @return string
181
	 */
182
	protected function getWorkingCopyPath()
183
	{
184
		return $this->_workingCopyResolver->getWorkingCopyPath($this->getRawPath());
185
	}
186
187
	/**
188
	 * Returns all refs.
189
	 *
190
	 * @return array
191
	 */
192
	protected function getAllRefs()
193
	{
194
		$wc_url = $this->getWorkingCopyUrl();
195
		$revision_log = $this->getRevisionLog($wc_url);
196
197
		return $revision_log->find('refs', 'all_refs');
198
	}
199
200
	/**
201
	 * Returns working copy path as used specified it.
202
	 *
203
	 * @return string
204
	 */
205
	protected function getRawPath()
206
	{
207
		if ( !isset($this->_rawPath) ) {
208
			// FIXME: During auto-complete working copy at CWD is used regardless of given path.
209
			if ( !isset($this->io) ) {
210
				$this->_rawPath = '.';
211
			}
212
			else {
213
				$this->_rawPath = $this->io->getArgument('path');
214
			}
215
		}
216
217
		return $this->_rawPath;
218
	}
219
220
}
221