SelfUpdate::execute()   B
last analyzed

Complexity

Conditions 7
Paths 13

Size

Total Lines 50
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 32
nc 13
nop 2
dl 0
loc 50
ccs 0
cts 31
cp 0
crap 56
rs 8.4746
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <[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
declare(strict_types=1);
13
14
namespace Cecil\Command;
15
16
use Humbug\SelfUpdate\Updater;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
/**
22
 * SelfUpdate command.
23
 *
24
 * This command checks for a newer version of Cecil and, if found, downloads and installs the latest version.
25
 * It can also revert to a previous version or force an update to the last stable or unstable version.
26
 */
27
class SelfUpdate extends AbstractCommand
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function configure()
33
    {
34
        $this
35
            ->setName('self-update')
36
            ->setAliases(['selfupdate'])
37
            ->setDescription('Updates Cecil to the latest version')
38
            ->setDefinition([
39
                new InputOption('rollback', null, InputOption::VALUE_NONE, 'Revert to an older installation'),
40
                new InputOption('stable', null, InputOption::VALUE_NONE, 'Force an update to the last stable version'),
41
                new InputOption('preview', null, InputOption::VALUE_NONE, 'Force an update to the last unstable version'),
42
            ])
43
            ->setHelp(
44
                <<<'EOF'
45
The <info>%command.name%</> command checks for a newer version and, if found, downloads and installs the latest.
46
47
  <info>%command.full_name%</>
48
49
To rollback to the <comment>previous</comment> version, run:
50
51
  <info>%command.full_name% --rollback</>
52
53
To update Cecil to the last <comment>stable</comment> version, run:
54
55
  <info>%command.full_name% --stable</>
56
57
To update Cecil to the last <comment>unstable</comment> version, run:
58
59
  <info>%command.full_name% --preview</>
60
EOF
61
            );
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    protected function execute(InputInterface $input, OutputInterface $output): int
68
    {
69
        $version = $this->getApplication()->getVersion();
70
71
        $updater = new Updater(null, false, Updater::STRATEGY_GITHUB);
72
73
        // rollback
74
        if ($input->getOption('rollback')) {
75
            try {
76
                $result = $updater->rollback();
77
                if (!$result) {
78
                    $output->writeln('Rollback failed.');
79
80
                    return 1;
81
                }
82
                $output->writeln('Rollback done.');
83
84
                return 0;
85
            } catch (\Exception $e) {
86
                $output->writeln($e->getMessage());
87
88
                return 1;
89
            }
90
        }
91
92
        /** @var \Humbug\SelfUpdate\Strategy\GithubStrategy $strategy */
93
        $strategy = $updater->getStrategy();
94
        $strategy->setPackageName('cecil/cecil');
95
        $strategy->setPharName('cecil.phar');
96
        $strategy->setCurrentLocalVersion($version);
97
        $strategy->setStability($input->getOption('preview') ? 'unstable' : 'stable');
98
        $updater->setStrategyObject($strategy);
99
100
        try {
101
            $output->writeln('Checking for updates...');
102
            $result = $updater->update();
103
            if ($result) {
104
                $new = $updater->getNewVersion();
105
                $old = $updater->getOldVersion();
106
                $output->writeln(\sprintf('Updated from <comment>%s</comment> to <info>%s</info>.', $old, $new));
107
108
                return 0;
109
            }
110
            $output->writeln(\sprintf('You are already using the last version (<comment>%s</comment>).', $version));
111
112
            return 0;
113
        } catch (\Exception $e) {
114
            $output->writeln($e->getMessage());
115
116
            return 1;
117
        }
118
    }
119
}
120