GenerateSpecCommand::validateVersion()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 3
c 1
b 1
f 0
nc 2
nop 2
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace Sugarcrm\UpgradeSpec\Command;
4
5
use Sugarcrm\UpgradeSpec\Context\Build;
6
use Sugarcrm\UpgradeSpec\Context\Target;
7
use Sugarcrm\UpgradeSpec\Data\Manager;
8
use Sugarcrm\UpgradeSpec\Helper\File;
9
use Sugarcrm\UpgradeSpec\Helper\Sugarcrm;
10
use Sugarcrm\UpgradeSpec\Context\Upgrade;
11
use Sugarcrm\UpgradeSpec\Spec\Generator;
12
use Sugarcrm\UpgradeSpec\Version\Version;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class GenerateSpecCommand extends Command
19
{
20
    /**
21
     * @var Generator
22
     */
23
    private $generator;
24
25
    /**
26
     * @var Sugarcrm
27
     */
28
    private $sugarcrm;
29
30
    /**
31
     * @var File
32
     */
33
    private $file;
34
35
    /**
36
     * @var Manager
37
     */
38
    private $dataManager;
39
40
    /**
41
     * GenerateSpecCommand constructor.
42
     *
43
     * @param null      $name
44
     * @param Generator $generator
45
     * @param Manager   $dataManager
46
     */
47
    public function __construct($name, Generator $generator, Manager $dataManager)
48
    {
49
        parent::__construct($name);
50
51
        $this->generator = $generator;
52
        $this->dataManager = $dataManager;
53
        $this->sugarcrm = new Sugarcrm();
54
        $this->file = new File();
55
    }
56
57
    /**
58
     *  Configure the command.
59
     */
60
    protected function configure()
61
    {
62
        $this->setName('generate:spec')
63
            ->setDescription('Generate a new upgrade spec')
64
            ->addArgument('path', InputArgument::REQUIRED, 'Path to SugarCRM build we are going to upgrade')
65
            ->addArgument('version', InputArgument::OPTIONAL, 'Version to upgrade to')
66
            ->addOption('dump', 'D', InputOption::VALUE_NONE, 'Save generated spec to file')
67
            ->addOption('upgradeSource', 'U', InputOption::VALUE_OPTIONAL, 'Path to a folder with SugarCRM upgrade packages');
68
    }
69
70
    /**
71
     * Execute the command.
72
     *
73
     * @param InputInterface  $input
74
     * @param OutputInterface $output
75
     *
76
     * @return int
77
     */
78
    protected function execute(InputInterface $input, OutputInterface $output)
79
    {
80
        $path = $input->getArgument('path');
81
82
        $version = $this->sugarcrm->getBuildVersion($path);
83
        $flav = $this->sugarcrm->getBuildFlav($path);
84
        $targetVersion = $input->getArgument('version') ? new Version($input->getArgument('version')) : null;
85
        $upgradeTo = $this->dataManager->getLatestVersion($flav, $targetVersion);
86
87
        $upgradeSource = null;
88
        if ($input->hasParameterOption('--upgradeSource') || $input->hasParameterOption('-U')) {
89
            $upgradeSource = $input->getOption('upgradeSource');
90
            if (!file_exists($upgradeSource) || !is_readable($upgradeSource)) {
91
                throw new \InvalidArgumentException('Invalid "upgradePackages" argument value');
92
            }
93
        }
94
95
        $upgradeContext = new Upgrade(
96
            new Build($version, $flav, $path),
97
            new Target($upgradeTo, $flav, $upgradeSource)
98
        );
99
100
        $output->writeln(sprintf('<comment>Generating upgrade spec: %s ...</comment>', $upgradeContext));
101
102
        $spec = $this->generator->generate($upgradeContext);
103
104
        if ($input->hasParameterOption('--dump') || $input->hasParameterOption('-D')) {
105
            $this->file->saveToFile(sprintf('%s.md', $upgradeContext->asFilename()), $spec);
106
        } else {
107
            $output->writeln(sprintf('<info>%s</info>', $spec));
108
        }
109
110
        $output->writeln('<comment>Done</comment>');
111
112
        return 0;
113
    }
114
115
    /**
116
     * @param InputInterface $input
117
     */
118
    protected function validateInput(InputInterface $input)
119
    {
120
        $path = $input->getArgument('path');
121
        $version = $input->getArgument('version') ? new Version($input->getArgument('version')) : null;
122
123
        $this->validatePath($path);
124
        $this->validateVersion(
125
            $this->sugarcrm->getBuildVersion($path),
126
            $this->dataManager->getLatestVersion($this->sugarcrm->getBuildFlav($path), $version)
127
        );
128
    }
129
130
    /**
131
     * @param $path
132
     */
133
    private function validatePath($path)
134
    {
135
        if (!$this->sugarcrm->isSugarcrmBuild($path)) {
136
            throw new \InvalidArgumentException('Invalid "path" argument value');
137
        }
138
    }
139
140
    /**
141
     * @param Version $from
142
     * @param Version $to
143
     */
144
    private function validateVersion(Version $from, Version $to)
145
    {
146
        if (version_compare((string) $to->getFull(), (string) $from->getFull(), '<=')) {
147
            throw new \InvalidArgumentException(sprintf('Given version ("%s") is lower or equal to the build version ("%s")', $to, $from));
148
        }
149
    }
150
}
151