Completed
Push — master ( 031efe...bebf49 )
by Mike
17:56 queued 11:36
created

SelfUpdateCommand::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 17
rs 9.4285
1
<?php
2
3
namespace Sugarcrm\UpgradeSpec\Command;
4
5
use Sugarcrm\UpgradeSpec\Updater\Updater;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class SelfUpdateCommand extends Command implements PharContext
11
{
12
    /**
13
     * @var Updater
14
     */
15
    private $updater;
16
17
    /**
18
     * SelfUpdateCommand constructor.
19
     *
20
     * @param null    $name
21
     * @param Updater $updater
22
     */
23
    public function __construct($name, Updater $updater)
24
    {
25
        parent::__construct($name);
26
27
        $this->updater = $updater;
28
    }
29
30
    /**
31
     * Configure the command.
32
     */
33
    protected function configure()
34
    {
35
        $this->setName('self:update')
36
            ->setDescription('Update uspec to the latest version')
37
            ->addOption('stability', 's',
38
                InputOption::VALUE_OPTIONAL,
39
                'Release stability (stable, unstable, any)',
40
                Updater::STABILITY_ANY
41
            )
42
            ->addOption('rollback', 'r', InputOption::VALUE_NONE, 'Rollback uspec update');
43
    }
44
45
    /**
46
     * Execute the command.
47
     *
48
     * @param InputInterface  $input
49
     * @param OutputInterface $output
50
     *
51
     * @return int
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        if ($input->hasParameterOption('--rollback') || $input->hasParameterOption('-r')) {
56
            return $this->rollback($input, $output);
57
        }
58
59
        return $this->update($input, $output);
60
    }
61
62
    /**
63
     * Validate user input.
64
     *
65
     * @param InputInterface $input
66
     */
67
    protected function validateInput(InputInterface $input)
68
    {
69
        $this->validateStability($input->getOption('stability'));
70
    }
71
72
    /**
73
     * @param $stability
74
     */
75
    private function validateStability($stability)
76
    {
77 View Code Duplication
        if (!in_array($stability, [
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
            Updater::STABILITY_STABLE,
79
            Updater::STABILITY_UNSTABLE,
80
            Updater::STABILITY_ANY,
81
        ])) {
82
            throw new \InvalidArgumentException('Invalid "stability" option value');
83
        }
84
    }
85
86
    /**
87
     * @param InputInterface $input
88
     * @param OutputInterface $output
89
     * @return int
90
     */
91
    private function update(InputInterface $input, OutputInterface $output)
92
    {
93
        if (!$this->updater->hasUpdate()) {
94
            $output->writeln('<info>No update needed!</info>');
95
96
            return 0;
97
        }
98
99
        $this->updater->update($input->getOption('stability'));
100
101
        $output->writeln(sprintf('<info>Successfully updated from "%s" to "%s"</info>',
102
            $this->updater->getOldVersion(),
103
            $this->updater->getNewVersion()
104
        ));
105
106
        return 0;
107
    }
108
109
    /**
110
     * @param InputInterface $input
111
     * @param OutputInterface $output
112
     * @return int
113
     */
114
    private function rollback(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
115
    {
116
        if (!$this->updater->rollback()) {
117
            $output->writeln('<error>Before rollback you need to perform at least one update.</error>');
118
119
            return 1;
120
        }
121
122
        $output->writeln('<info>Success!</info>');
123
124
        return 0;
125
    }
126
}
127