Completed
Push — master ( f2c904...ce3ac2 )
by Alexander
03:05
created

SelfUpdateCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
ccs 0
cts 12
cp 0
rs 9.4285
cc 2
eloc 8
nc 2
nop 2
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 Humbug\SelfUpdate\Strategy\GithubStrategy;
17
use Humbug\SelfUpdate\Strategy\ShaStrategy;
18
use Humbug\SelfUpdate\Strategy\StrategyInterface;
19
use Humbug\SelfUpdate\Updater;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
class SelfUpdateCommand extends AbstractCommand
25
{
26
27
	const UPDATE_CHANNEL_SNAPSHOT = 'snapshot';
28
29
	const UPDATE_CHANNEL_STABLE = 'stable';
30
31
	const UPDATE_SERVER_URL = 'https://svn-buddy-updater.herokuapp.com';
32
33
	/**
34
	 * Config editor.
35
	 *
36
	 * @var ConfigEditor
37
	 */
38
	private $_configEditor;
39
40
	/**
41
	 * Path, where to store backups.
42
	 *
43
	 * @var string
44
	 */
45
	private $_backupsPath;
46
47
	/**
48
	 * {@inheritdoc}
49
	 */
50
	protected function configure()
51
	{
52
		$this
53
			->setName('self-update')
54
			->setDescription(
55
				'Updates application to most recent version in a given stability channel'
56
			)
57
			->addOption(
58
				'stable',
59
				null,
60
				InputOption::VALUE_NONE,
61
				'Force an update to the stable channel'
62
			)
63
			->addOption(
64
				'snapshot',
65
				null,
66
				InputOption::VALUE_NONE,
67
				'Force an update to the snapshot channel'
68
			);
69
70
		parent::configure();
71
	}
72
73
	/**
74
	 * Prepare dependencies.
75
	 *
76
	 * @return void
77
	 */
78
	protected function prepareDependencies()
79
	{
80
		parent::prepareDependencies();
81
82
		$container = $this->getContainer();
83
84
		$this->_configEditor = $container['config_editor'];
85
		$this->_backupsPath = $container['working_directory'] . '/backups';
86
87
		if ( !file_exists($this->_backupsPath) ) {
88
			mkdir($this->_backupsPath, 0777, true);
89
		}
90
	}
91
92
	/**
93
	 * {@inheritdoc}
94
	 */
95
	protected function execute(InputInterface $input, OutputInterface $output)
96
	{
97
		$updater = new Updater(null, false);
98
		$updater->setStrategyObject($this->getUpdateStrategy());
99
100
		$result = $updater->update();
101
102
		if ( !$result ) {
103
			$this->io->writeln('Already using latest version ("' . $this->getUpdateChannel() . '" update channel).');
104
		}
105
		else {
106
			$this->io->writeln('Updated to latest version ("' . $this->getUpdateChannel() . '" update channel).');
107
		}
108
	}
109
110
	/**
111
	 * Returns update strategy.
112
	 *
113
	 * @return StrategyInterface
114
	 * @throws CommandException When update channel cannot be found.
115
	 */
116
	protected function getUpdateStrategy()
117
	{
118
		$update_channel = $this->getUpdateChannel();
119
120
		if ( $update_channel === self::UPDATE_CHANNEL_STABLE ) {
121
			$update_strategy = new GithubStrategy();
122
			$update_strategy->setPackageName('console-helpers/svn-buddy');
123
			$update_strategy->setPharName('svn-buddy.phar');
124
			$update_strategy->setCurrentLocalVersion($this->getApplication()->getVersion());
125
126
			return $update_strategy;
127
		}
128
129
		$versions = json_decode(
130
			humbug_get_contents(self::UPDATE_SERVER_URL . '/versions'),
131
			true
132
		);
133
134
		if ( !isset($versions[$update_channel]) ) {
135
			throw new CommandException('The "' . $update_channel . '" update channel not found.');
136
		}
137
138
		$update_strategy = new ShaStrategy();
139
		$update_strategy->setPharUrl(self::UPDATE_SERVER_URL . $versions[$update_channel]['path']);
140
		$update_strategy->setVersionUrl(self::UPDATE_SERVER_URL . $versions[$update_channel]['path'] . '.sig');
141
142
		return $update_strategy;
143
	}
144
145
	/**
146
	 * Returns update channel.
147
	 *
148
	 * @return string
149
	 */
150
	protected function getUpdateChannel()
151
	{
152
		if ( $this->io->getOption('stable') ) {
153
			$this->_configEditor->set('update-channel', self::UPDATE_CHANNEL_STABLE);
154
		}
155
156
		if ( $this->io->getOption('snapshot') ) {
157
			$this->_configEditor->set('update-channel', self::UPDATE_CHANNEL_SNAPSHOT);
158
		}
159
160
		return $this->_configEditor->get('update-channel');
161
	}
162
163
}
164