Completed
Push — develop ( 5bf394...6bbee1 )
by Tom
06:47
created

DowngradeVersionsCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
c 1
b 0
f 1
lcom 1
cbo 5
dl 0
loc 105
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 12 1
A execute() 0 15 3
B processModule() 0 14 5
A needsDowngrade() 0 21 2
A getModuleVersions() 0 4 1
1
<?php
2
/*
3
 * @author Tom Klingenberg <https://github.com/ktomk>
4
 */
5
6
namespace N98\Magento\Command\System\Setup;
7
8
use N98\Magento\Api\Module;
9
use N98\Magento\Api\ModuleInterface;
10
use N98\Magento\Api\ModuleListVersionIterator;
11
use N98\Magento\Api\ModuleVersion;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
class DowngradeVersionsCommand extends AbstractSetupCommand
18
{
19
    /**
20
     * @var OutputInterface
21
     */
22
    private $output;
23
24
    /**
25
     * @var bool
26
     */
27
    private $dryRun;
28
29
    /**
30
     * Setup
31
     */
32
    protected function configure()
33
    {
34
        $this
35
            ->setName('sys:setup:downgrade-versions')
36
            ->addOption('dry-run', null, InputOption::VALUE_NONE, 'write what to change but do not do any changes')
37
            ->setDescription('Automatically downgrade schema and module versions');
38
        $help
39
            = <<<HELP
40
If version numbers are too high - normally happens while developing - this command will lower them to the expected ones.
41
HELP;
42
        $this->setHelp($help);
43
    }
44
45
    /**
46
     * @param InputInterface $input
47
     * @param OutputInterface $output
48
     *
49
     * @return int|null|void
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        $this->detectMagento($output, true);
54
55
        if (!$this->initMagento()) {
56
            return;
57
        }
58
59
        $this->output = $output;
60
        $this->dryRun = $input->getOption('dry-run');
61
62
        foreach ($this->getModuleVersions() as $moduleVersion) {
63
            $this->processModule($moduleVersion);
64
        }
65
    }
66
67
    /**
68
     * @param $module
69
     */
70
    private function processModule(ModuleVersion $module)
71
    {
72
        $dryRun = $this->dryRun;
73
74
        // data version
75
        if ($this->needsDowngrade($module, 'data', $module->getDataVersion())) {
76
            $dryRun || $module->setDataVersion($module->getVersion());
77
        }
78
79
        // db version
80
        if ($this->needsDowngrade($module, 'db', $module->getDbVersion())) {
81
            $dryRun || $module->setDbVersion($module->getVersion());
82
        }
83
    }
84
85
    /**
86
     * @param ModuleInterface $module
87
     * @param string $what
88
     * @param string $currentVersion
89
     *
90
     * @return bool
91
     */
92
    private function needsDowngrade(ModuleInterface $module, $what, $currentVersion)
93
    {
94
        $targetVersion = $module->getVersion();
95
        $needsDowngrade = 1 === version_compare($currentVersion, $targetVersion);
96
97
        if (!$needsDowngrade) {
98
            return false;
99
        }
100
101
        $this->output->writeln(
102
            sprintf(
103
                "<info>Change module '%s' %s-version from %s to %s.</info>",
104
                $module->getName(),
105
                $what,
106
                $currentVersion,
107
                $targetVersion
108
            )
109
        );
110
111
        return true;
112
    }
113
114
    /**
115
     * @return ModuleListVersionIterator
116
     */
117
    private function getModuleVersions()
118
    {
119
        return new ModuleListVersionIterator($this->moduleList, $this->resource);
120
    }
121
}
122