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