SelfUpdateCommand::execute()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
ccs 0
cts 20
cp 0
rs 8.8571
cc 3
eloc 17
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Tivnet\WPDB\Command;
4
5
use Herrera\Phar\Update\Manager;
6
use Herrera\Phar\Update\Manifest;
7
use Herrera\Json\Exception\FileException;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * Class SelfUpdateCommand
15
 * @package Tivnet\WPDB\Command
16
 */
17
class SelfUpdateCommand extends Command {
18
19
	/**
20
	 * Update path
21
	 */
22
	const MANIFEST_FILE = 'http://tivnet.github.io/wpdb/manifest.json';
23
24
	/**
25
	 * Configuration
26
	 * @return void
27
	 */
28
	protected function configure() {
29
		$this
30
			->setName( 'selfupdate' )
31
			->setDescription( 'Updates wpdb.phar to the latest version' )
32
			->addOption( 'major', null, InputOption::VALUE_NONE, 'Allow major version update' );
33
	}
34
35
	/**
36
	 * Executes the update command
37
	 *
38
	 * @param InputInterface  $input
39
	 * @param OutputInterface $output
40
	 *
41
	 * @return null|int
42
	 */
43
	protected function execute( InputInterface $input, OutputInterface $output ) {
44
		$output->writeln( 'Looking for updates...' );
45
46
		try {
47
			$manager = new Manager( Manifest::loadFile( self::MANIFEST_FILE ) );
48
		} catch ( FileException $e ) {
49
			$output->writeln( /** @lang text */
50
				'<error>Updates could not be fetched</error>' );
51
52
			return 1;
53
		}
54
55
		$currentVersion = $this->getApplication()->getVersion();
56
		$allowMajor     = $input->getOption( 'major' );
57
58
		if ( $manager->update( $currentVersion, $allowMajor ) ) {
59
			$output->writeln( /** @lang text */
60
				'<info>Updated to latest version</info>' );
61
		} else {
62
			$output->writeln( /** @lang text */
63
				'<comment>Already up-to-date</comment>' );
64
		}
65
66
		return null;
67
	}
68
}
69