Passed
Push — master ( 2b0e69...0701c6 )
by Luis
39s queued 11s
created

SelfUpdateCommand::configureUpdaterStrategy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 2
b 0
f 0
1
<?php
2
/**
3
 * PHP version 7.1
4
 *
5
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
6
 */
7
8
namespace PhUml\Console\Commands;
9
10
use Exception;
11
use Humbug\SelfUpdate\Strategy\ShaStrategy;
0 ignored issues
show
Bug introduced by
The type Humbug\SelfUpdate\Strategy\ShaStrategy was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Humbug\SelfUpdate\Updater;
0 ignored issues
show
Bug introduced by
The type Humbug\SelfUpdate\Updater was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use PhUml\Console\UpdaterDisplay;
14
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Symfony\Component\Console\Input\InputOption;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputOption was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
class SelfUpdateCommand extends Command
20
{
21
    private const VERSION_URL = 'https://montealegreluis.com/phuml/phuml.phar.version';
22
23
    private const PHAR_URL = 'https://montealegreluis.com/phuml/phuml.phar';
24
25
    private const SUCCESS = 0;
26
27
    /** @var Updater */
28
    private $updater;
29
30
    /** @var UpdaterDisplay */
31 24
    private $display;
32
33 24
    /** @throws \Symfony\Component\Console\Exception\LogicException */
34 24
    public function __construct(Updater $updater, UpdaterDisplay $display)
35 24
    {
36 24
        parent::__construct();
37
        $this->updater = $updater;
38 24
        $this->display = $display;
39
    }
40
41 24
    protected function configure(): void
42 24
    {
43 24
        $this
44 24
            ->setName('self-update')
45 24
            ->setDescription('Update phuml.phar to most recent stable version.')
46 24
            ->addOption(
47 24
                'rollback',
48
                'r',
49 24
                InputOption::VALUE_NONE,
50 24
                'Rollback to previous version of phUML if available on filesystem.'
51 24
            )
52 24
            ->addOption(
53 24
                'check',
54
                'c',
55 24
                InputOption::VALUE_NONE,
56
                'Checks if there is an updated version available.'
57 24
            );
58
    }
59 24
60
    protected function execute(InputInterface $input, OutputInterface $output): int
61 24
    {
62 9
        $this->configureUpdaterStrategy();
63 9
64
        if (true === (bool)$input->getOption('rollback')) {
65
            return $this->tryToRollback();
66 15
        }
67 9
68 9
        if (true === (bool)$input->getOption('check')) {
69
            return $this->tryToCheckForUpdates();
70
        }
71 6
72 6
        return $this->tryToUpdate($output);
73
    }
74 9
75
    private function tryToRollback(): int
76 9
    {
77 9
        $this->tryAction([$this, 'rollback']);
78
79 9
        return self::SUCCESS;
80
    }
81 9
82 6
    private function rollback(): void
83 6
    {
84
        $result = $this->updater->rollback();
85 9
        $this->display->rollbackMessage($result);
86
    }
87 9
88 9
    private function tryToCheckForUpdates(): int
89 9
    {
90
        $this->display->currentLocalVersion($this->getApplication()->getVersion());
91 9
        $this->tryAction([$this, 'showAvailableUpdates']);
92
93 9
        return self::SUCCESS;
94 3
    }
95 6
96 3
    private function showAvailableUpdates(): void
97
    {
98 3
        if ($this->updater->hasUpdate()) {
99
            $this->display->newVersion($this->updater->getNewVersion());
100 9
        } elseif (false === $this->updater->getNewVersion()) {
101
            $this->display->noUpdatesAvailable();
102 6
        } else {
103
            $this->display->alreadyUpToDate();
104 6
        }
105 6
    }
106 6
107 6
    private function tryToUpdate(OutputInterface $output): int
108
    {
109 6
        $output->writeln('Updating...' . PHP_EOL);
110
        $this->tryAction([$this, 'update']);
111 6
        $output->write(PHP_EOL);
112 6
113 3
        return self::SUCCESS;
114
    }
115 3
116
    private function update(): void
117 6
    {
118
        $result = $this->updater->update();
119 24
        if ($result) {
120
            $this->display->updateApplied($this->updater->getOldVersion(), $this->updater->getNewVersion());
121 24
        } else {
122 24
            $this->display->noUpdateApplied($this->updater->getOldVersion());
123 24
        }
124 24
    }
125 24
126
    private function configureUpdaterStrategy(): void
127 24
    {
128
        $strategy = new ShaStrategy();
129
        $strategy->setPharUrl(self::PHAR_URL);
130 24
        $strategy->setVersionUrl(self::VERSION_URL);
131 3
        $this->updater->setStrategyObject($strategy);
132 3
    }
133
134 24
    private function tryAction(callable $action): void
135
    {
136
        try {
137
            $action();
138
        } catch (Exception $exception) {
139
            $this->display->error($exception);
140
        }
141
    }
142
}
143