SelfUpdateCommandHandler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 6
c 6
b 0
f 2
lcom 0
cbo 4
dl 0
loc 53
ccs 0
cts 32
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getStability() 0 14 4
B handle() 0 27 2
1
<?php
2
3
/*
4
 * This file is part of the puli/cli package.
5
 *
6
 * (c) Bernhard Schussek <[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 Puli\Cli\Handler;
13
14
use Humbug\SelfUpdate\Updater;
15
use Puli\Cli\PuliApplicationConfig;
16
use Puli\Cli\Updater\PuliStrategy;
17
use Webmozart\Console\Api\Args\Args;
18
use Webmozart\Console\Api\IO\IO;
19
20
/**
21
 * Handles the "self-update" command.
22
 *
23
 * @since  1.0
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 */
27
class SelfUpdateCommandHandler
28
{
29
    /**
30
     * Handles the "self-update" command.
31
     *
32
     * @param Args $args The console arguments.
33
     * @param IO   $io   The I/O.
34
     *
35
     * @return int The status code.
36
     */
37
    public function handle(Args $args, IO $io)
38
    {
39
        $updateStrategy = new PuliStrategy();
40
        $updateStrategy->setStability($this->getStability($args));
41
42
        // false: disable signed releases, otherwise the updater will look for
43
        // a *.pubkey file for the PHAR
44
        $updater = new Updater(null, false);
45
        $updater->setStrategyObject($updateStrategy);
46
47
        if ($updater->update()) {
48
            $io->writeLine(sprintf(
49
                'Updated from version %s to version %s.',
50
                $updater->getOldVersion(),
51
                $updater->getNewVersion()
52
            ));
53
54
            return 0;
55
        }
56
57
        $io->writeLine(sprintf(
58
            'Version %s is the latest version. No update required.',
59
            $updater->getOldVersion()
60
        ));
61
62
        return 0;
63
    }
64
65
    private function getStability(Args $args)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
66
    {
67
        if ($args->isOptionSet('stable')) {
68
            return PuliStrategy::STABLE;
69
        }
70
71
        if ($args->isOptionSet('unstable')) {
72
            return PuliStrategy::UNSTABLE;
73
        }
74
75
        return preg_match('/\-(dev|alpha|beta)/', PuliApplicationConfig::VERSION)
76
            ? PuliStrategy::UNSTABLE
77
            : PuliStrategy::STABLE;
78
    }
79
}
80