Completed
Push — master ( 95a54c...7178fa )
by Samuel
10:58
created

SelfUpdateCommand::execute()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 15
nc 3
nop 2
1
<?php
2
3
namespace Dock\Cli;
4
5
use Herrera\Phar\Update\Manager;
6
use Symfony\Component\Console\Input\InputOption;
7
use Herrera\Json\Exception\FileException;
8
use Herrera\Phar\Update\Manifest;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class SelfUpdateCommand extends Command
14
{
15
    const MANIFEST_FILE = 'http://inviqa.github.io/dock-cli/manifest.json';
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected function configure()
21
    {
22
        $this
23
            ->setName('self-update')
24
            ->setDescription('Updates Dock CLI to the latest version')
25
            ->addOption('major', null, InputOption::VALUE_NONE, 'Allow major version update')
26
            ->addOption('disallow-unstable', null, InputOption::VALUE_NONE, 'Disallow unstable versions')
27
        ;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected function execute(InputInterface $input, OutputInterface $output)
34
    {
35
        $output->writeln('Looking for updates...');
36
37
        try {
38
            $manager = new Manager(Manifest::loadFile(self::MANIFEST_FILE));
39
        } catch (FileException $e) {
40
            $output->writeln('<error>Unable to search for updates</error>');
41
42
            return 1;
43
        }
44
45
        $currentVersion = $this->getApplication()->getVersion();
46
        $allowMajor = $input->getOption('major');
47
        $allowUnstableVersions = !$input->getOption('disallow-unstable');
48
49
        if ($manager->update($currentVersion, $allowMajor, $allowUnstableVersions)) {
50
            $output->writeln('<info>Updated to latest version</info>');
51
        } else {
52
            $output->writeln('<comment>Already up-to-date</comment>');
53
        }
54
55
        return 0;
56
    }
57
}
58