Completed
Pull Request — master (#602)
by Richard
14:48
created

DeactivateModuleCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A execute() 0 23 3
1
<?php
2
3
namespace XoopsConsole\Commands;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
use Xoops\Core\XoopsTpl;
11
12
class DeactivateModuleCommand extends Command
13
{
14
    protected function configure()
15
    {
16
        $this->setName("deactivate-module")
17
            ->setDescription("Deactivate an installed module")
18
            ->setDefinition(array(
19
                new InputArgument('module', InputArgument::REQUIRED, 'Module directory name'),
20
            ))->setHelp(<<<EOT
21
The <info>deactivate-module</info> command deactivates a currently installed module.
22
EOT
23
            );
24
    }
25
26
    protected function execute(InputInterface $input, OutputInterface $output)
27
    {
28
        $module = $input->getArgument('module');
29
        $output->writeln(sprintf('Deactivating %s', $module));
0 ignored issues
show
Bug introduced by
It seems like $module can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
        $output->writeln(sprintf('Deactivating %s', /** @scrutinizer ignore-type */ $module));
Loading history...
30
        $xoops = \Xoops::getInstance();
31
        $moduleHandler = $xoops->getHandlerModule();
32
        $moduleObject = $moduleHandler->getByDirname($module);
0 ignored issues
show
Bug introduced by
It seems like $module can also be of type string[]; however, parameter $dirname of Xoops\Core\Kernel\Handle...Handler::getByDirname() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
        $moduleObject = $moduleHandler->getByDirname(/** @scrutinizer ignore-type */ $module);
Loading history...
33
        if (false === $moduleObject) {
34
            $output->writeln(sprintf('<error>%s is not an installed module!</error>', $module));
35
            return;
36
        }
37
        $moduleObject->setVar('isactive', false);
38
        $moduleHandler->insert($moduleObject, true);
39
40
        $blockHandler = $xoops->getHandlerBlock();
41
        $blocks = $blockHandler->getByModule($moduleObject->getVar('mid'));
42
        foreach ($blocks as $block) {
43
            /* @var $block \Xoops\Core\Kernel\Handlers\XoopsBlock */
44
            $block->setVar('isactive', false);
45
            $blockHandler->insert($block);
46
        }
47
        $output->writeln(sprintf('<info>Set %s module inactive</info>', $module));
48
        $xoops->cache()->delete('system');
49
    }
50
}
51