Passed
Push — master ( 0099d2...687a92 )
by Alexander
12:40 queued 10:31
created

UpdateCommand::deploy()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 16
ccs 0
cts 8
cp 0
rs 10
cc 3
nc 4
nop 0
crap 12
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\SVNBuddy\Config\AbstractConfigSetting;
15
use ConsoleHelpers\SVNBuddy\Config\ChoiceConfigSetting;
16
use ConsoleHelpers\SVNBuddy\Repository\WorkingCopyConflictTracker;
17
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
class UpdateCommand extends AbstractCommand implements IAggregatorAwareCommand, IConfigAwareCommand
24
{
25
26
	const SETTING_UPDATE_AUTO_DEPLOY = 'update.auto-deploy';
27
28
	/**
29
	 * Working copy conflict tracker.
30
	 *
31
	 * @var WorkingCopyConflictTracker
32
	 */
33
	private $_workingCopyConflictTracker;
34
35
	/**
36
	 * Prepare dependencies.
37
	 *
38
	 * @return void
39
	 */
40
	protected function prepareDependencies()
41
	{
42
		parent::prepareDependencies();
43
44
		$container = $this->getContainer();
45
46
		$this->_workingCopyConflictTracker = $container['working_copy_conflict_tracker'];
47
	}
48
49
	/**
50
	 * {@inheritdoc}
51
	 */
52
	protected function configure()
53
	{
54
		$this
55
			->setName('update')
56
			->setDescription('Bring changes from the repository into the working copy')
57
			->setAliases(array('up'))
58
			->addArgument(
59
				'path',
60
				InputArgument::OPTIONAL,
61
				'Working copy path',
62
				'.'
63
			)
64
			->addOption(
65
				'revision',
66
				'r',
67
				InputOption::VALUE_REQUIRED,
68
				'Update working copy to specified revision, e.g. <comment>NUMBER</comment>, <comment>{DATE}</comment>, <comment>HEAD</comment>, <comment>BASE</comment>, <comment>COMMITTED</comment>, <comment>PREV</comment>'
69
			)
70
			->addOption(
71
				'ignore-externals',
72
				null,
73
				InputOption::VALUE_NONE,
74
				'Ignore externals definitions'
75
			)
76
			->addOption(
77
				'auto-deploy',
78
				null,
79
				InputOption::VALUE_REQUIRED,
80
				'Automatically perform local deployment on successful update, e.g. <comment>yes</comment> or <comment>no</comment>'
81
			);
82
83
		parent::configure();
84
	}
85
86
	/**
87
	 * Return possible values for the named option
88
	 *
89
	 * @param string            $optionName Option name.
90
	 * @param CompletionContext $context    Completion context.
91
	 *
92
	 * @return array
93
	 */
94
	public function completeOptionValues($optionName, CompletionContext $context)
95
	{
96
		$ret = parent::completeOptionValues($optionName, $context);
97
98
		if ( $optionName === 'auto-deploy' ) {
99
			return array('yes', 'no');
100
		}
101
102
		return $ret;
103
	}
104
105
	/**
106
	 * {@inheritdoc}
107
	 */
108
	protected function execute(InputInterface $input, OutputInterface $output)
109
	{
110
		$wc_path = $this->getWorkingCopyPath();
111
		$revision = $this->io->getOption('revision');
112
		$ignore_externals = $this->io->getOption('ignore-externals');
113
114
		$show_revision = $revision ? $revision : 'HEAD';
115
		$show_externals = $ignore_externals ? '(excluding externals)' : '(including externals)';
116
		$this->io->writeln(
117
			'Updating working copy to <info>' . $show_revision . '</info> revision ' . $show_externals . ' ... '
0 ignored issues
show
Bug introduced by
Are you sure $show_revision of type string|string[]|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
			'Updating working copy to <info>' . /** @scrutinizer ignore-type */ $show_revision . '</info> revision ' . $show_externals . ' ... '
Loading history...
118
		);
119
120
		$arguments = array($wc_path);
121
122
		if ( $revision ) {
123
			$arguments[] = '--revision';
124
			$arguments[] = $revision;
125
		}
126
127
		if ( $ignore_externals ) {
128
			$arguments[] = '--ignore-externals';
129
		}
130
131
		$command = $this->repositoryConnector->getCommand('update', $arguments);
132
		$command->runLive(array(
133
			$wc_path => '.',
134
		));
135
136
		if ( $this->_workingCopyConflictTracker->getNewConflicts($wc_path) ) {
137
			$this->_workingCopyConflictTracker->add($wc_path);
138
		}
139
140
		$this->io->writeln('<info>Done</info>');
141
142
		$this->deploy();
143
	}
144
145
	/**
146
	 * Performs a deploy.
147
	 *
148
	 * @return void
149
	 */
150
	protected function deploy()
151
	{
152
		$auto_deploy = $this->io->getOption('auto-deploy');
153
154
		if ( $auto_deploy !== null ) {
155
			$auto_deploy = $auto_deploy === 'yes';
156
		}
157
		else {
158
			$auto_deploy = (boolean)$this->getSetting(self::SETTING_UPDATE_AUTO_DEPLOY);
159
		}
160
161
		if ( !$auto_deploy ) {
162
			return;
163
		}
164
165
		$this->runOtherCommand('deploy', array('--local' => true));
166
	}
167
168
	/**
169
	 * Returns option names, that makes sense to use in aggregation mode.
170
	 *
171
	 * @return array
172
	 */
173
	public function getAggregatedOptions()
174
	{
175
		return array('ignore-externals');
176
	}
177
178
	/**
179
	 * Returns list of config settings.
180
	 *
181
	 * @return AbstractConfigSetting[]
182
	 */
183
	public function getConfigSettings()
184
	{
185
		return array(
186
			new ChoiceConfigSetting(
187
				self::SETTING_UPDATE_AUTO_DEPLOY,
188
				array(1 => 'Yes', 0 => 'No'),
189
				1
190
			),
191
		);
192
	}
193
194
}
195