Completed
Pull Request — 1.x (#35)
by
unknown
18:26
created

SelfUpdateCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 3
c 2
b 0
f 2
lcom 0
cbo 6
dl 0
loc 40
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A execute() 0 20 2
1
<?php
2
3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CacheTool\Command;
13
14
use Herrera\Phar\Update\Manager;
15
use Herrera\Phar\Update\Manifest;
16
use Herrera\Phar\Update\Update;
17
use Herrera\Version\Parser;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
class SelfUpdateCommand extends Command
23
{
24
    const MANIFEST_FILE = 'http://gordalina.github.io/cachetool/manifest.json';
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected function configure()
30
    {
31
        $this
32
            ->setName('self-update')
33
            ->setAliases(array('selfupdate'))
34
            ->setDescription('Updates cachetool.phar to the latest version')
35
        ;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $manifest = Manifest::loadFile(self::MANIFEST_FILE);
44
45
        $currentVersion = Parser::toVersion($this->getApplication()->getVersion());
46
        $update = $manifest->findRecent($currentVersion, true);
47
48
        if (false === $update instanceof Update) {
49
            $output->writeln(sprintf('You are already using the latest version: <info>%s</info>', $currentVersion));
50
51
            return 0;
52
        }
53
54
        $output->writeln(sprintf('Updating to version <info>%s</info>', $update->getVersion()));
55
56
        $manager = new Manager($manifest);
57
        $manager->update($this->getApplication()->getVersion(), true);
58
59
        $output->writeln(sprintf('SHA1 verified <info>%s</info>', $update->getSha1()));
60
    }
61
}
62