Failed Conditions
Push — master ( 81451d...61709b )
by Alexander
03:23
created

SelfUpdateCommand::processUpdate()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 34
ccs 0
cts 24
cp 0
rs 8.8571
cc 2
eloc 21
nc 2
nop 0
crap 6
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\Config\ConfigEditor;
15
use ConsoleHelpers\ConsoleKit\Exception\CommandException;
16
use ConsoleHelpers\SVNBuddy\Updater\Stability;
17
use ConsoleHelpers\SVNBuddy\Updater\Updater;
18
use ConsoleHelpers\SVNBuddy\Updater\VersionUpdateStrategy;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
class SelfUpdateCommand extends AbstractCommand
24
{
25
26
	/**
27
	 * Config editor.
28
	 *
29
	 * @var ConfigEditor
30
	 */
31
	private $_configEditor;
32
33
	/**
34
	 * {@inheritdoc}
35
	 */
36
	protected function configure()
37
	{
38
		$this
39
			->setName('self-update')
40
			->setDescription(
41
				'Update application to most recent version'
42
			)
43
			->addOption(
44
				'rollback',
45
				'r',
46
				InputOption::VALUE_NONE,
47
				'Revert to an older version of the application'
48
			)
49
			->addOption(
50
				'stable',
51
				null,
52
				InputOption::VALUE_NONE,
53
				'Force an update to the stable channel'
54
			)
55
			->addOption(
56
				'snapshot',
57
				null,
58
				InputOption::VALUE_NONE,
59
				'Force an update to the snapshot channel'
60
			)
61
			->addOption(
62
				'preview',
63
				null,
64
				InputOption::VALUE_NONE,
65
				'Force an update to the preview channel'
66
			);
67
68
		parent::configure();
69
	}
70
71
	/**
72
	 * Prepare dependencies.
73
	 *
74
	 * @return void
75
	 */
76
	protected function prepareDependencies()
77
	{
78
		parent::prepareDependencies();
79
80
		$container = $this->getContainer();
81
82
		$this->_configEditor = $container['config_editor'];
83
	}
84
85
	/**
86
	 * {@inheritdoc}
87
	 */
88
	protected function execute(InputInterface $input, OutputInterface $output)
89
	{
90
		if ( $this->io->getOption('rollback') ) {
91
			$this->processRollback();
92
		}
93
		else {
94
			$this->processUpdate();
95
		}
96
	}
97
98
	/**
99
	 * Returns update channel.
100
	 *
101
	 * @return string
102
	 */
103
	protected function getUpdateChannel()
104
	{
105
		if ( $this->io->getOption('stable') ) {
106
			$this->_configEditor->set('update-channel', Stability::STABLE);
107
		}
108
109
		if ( $this->io->getOption('snapshot') ) {
110
			$this->_configEditor->set('update-channel', Stability::SNAPSHOT);
111
		}
112
113
		if ( $this->io->getOption('preview') ) {
114
			$this->_configEditor->set('update-channel', Stability::PREVIEW);
115
		}
116
117
		return $this->_configEditor->get('update-channel');
118
	}
119
120
	/**
121
	 * Processes rollback.
122
	 *
123
	 * @return void
124
	 * @throws CommandException When rollback failed.
125
	 */
126
	protected function processRollback()
127
	{
128
		$updater = new Updater(null, false);
129
130
		if ( !$updater->rollback() ) {
131
			throw new CommandException('Failed to restore previous version.');
132
		}
133
134
		$this->io->writeln('Rolling back to version <info>2016-05-10_15-21-19-1.1.0</info>.');
135
	}
136
137
	/**
138
	 * Processes update.
139
	 *
140
	 * @return void
141
	 */
142
	protected function processUpdate()
143
	{
144
		$update_strategy = new VersionUpdateStrategy();
145
		$update_channel = $this->getUpdateChannel();
146
		$update_strategy->setStability($update_channel);
147
		$update_strategy->setCurrentLocalVersion($this->getApplication()->getVersion());
148
149
		$updater = new Updater(null, false);
150
		$updater->setStrategyObject($update_strategy);
151
152
		if ( !$updater->hasUpdate() ) {
153
			$this->io->writeln(sprintf(
154
				'<info>You are already using version %s (%s channel).</info>',
155
				$updater->getNewVersion(),
156
				$update_channel
157
			));
158
159
			return;
160
		}
161
162
		$this->io->write(
163
			'Updating to version <info>' . $updater->getNewVersion() . '</info> (' . $update_channel . ' channel) ... '
164
		);
165
166
		$updater->update();
167
168
		$this->io->writeln('done.');
169
170
		$this->io->writeln(sprintf(
171
			'Use <info>%s self-update --rollback</info> to return to version %s',
172
			$_SERVER['argv'][0],
173
			$updater->getOldVersion()
174
		));
175
	}
176
177
}
178