Completed
Push — master ( d2354a...84b175 )
by Nik
02:25
created

LoadCommand::execute()   D

Complexity

Conditions 9
Paths 32

Size

Total Lines 41
Code Lines 24

Duplication

Lines 11
Ratio 26.83 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 11
loc 41
rs 4.909
c 2
b 1
f 0
cc 9
eloc 24
nc 32
nop 2
1
<?php
2
/**
3
 * For the full copyright and license information, please view the LICENSE.md
4
 * file that was distributed with this source code.
5
 */
6
7
namespace Notamedia\ConsoleJedi\Module\Command;
8
9
use Notamedia\ConsoleJedi\Application\CanRestartTrait;
10
use Notamedia\ConsoleJedi\Module\Exception\ModuleInstallException;
11
use Notamedia\ConsoleJedi\Module\Module;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * Command for module installation / register.
18
 *
19
 * @author Marat Shamshutdinov <[email protected]>
20
 */
21
class LoadCommand extends ModuleCommand
22
{
23
    use CanRestartTrait;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function configure()
29
    {
30
        parent::configure();
31
32
        $this->setName('module:load')
33
            ->setDescription('Load and install module from Marketplace')
34
            ->addOption('no-update', 'nu', InputOption::VALUE_NONE, 'Don\' update module')
35
            ->addOption('no-register', 'ni', InputOption::VALUE_NONE, 'Load only, don\' register module')
36
            ->addOption('beta', 'b', InputOption::VALUE_NONE, 'Allow the installation of beta releases');
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        $module = new Module($input->getArgument('module'));
45
46
        if (!$module->isThirdParty()) {
47
            $output->writeln('<info>Loading kernel modules is unsupported</info>');
48
        }
49
50
        if ($input->getOption('beta')) {
51
            $module->setBeta();
52
        }
53
54
        $module->load();
55
56
        if (!$input->getOption('no-update')) {
57
            $modulesUpdated = null;
58 View Code Duplication
            while ($module->update($modulesUpdated)) {
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...
59
                if (is_array($modulesUpdated)) {
60
                    foreach ($modulesUpdated as $moduleName => $moduleVersion) {
61
                        $output->writeln(sprintf('updated %s to <info>%s</info>', $moduleName, $moduleVersion));
62
                    }
63
                }
64
                return $this->restartScript($input, $output);
65
            }
66
        }
67
68
        if (!$input->getOption('no-register')) {
69
            try {
70
                $module->register();
71
            } catch (ModuleInstallException $e) {
72
                $output->writeln(sprintf('<comment>%s</comment>', $e->getMessage()),
73
                    OutputInterface::VERBOSITY_VERBOSE);
74
                $output->writeln(sprintf('Module loaded, but <error>not registered</error>. You need to do it yourself in admin panel.',
75
                    $module->getName()));
76
            }
77
        }
78
79
        $output->writeln(sprintf('installed <info>%s</info>', $module->getName()));
80
81
        return 0;
82
    }
83
}