DisableModuleCommand::execute()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
c 2
b 0
f 0
dl 0
loc 36
rs 9.2728
cc 5
nc 5
nop 2
1
<?php
2
3
/**
4
 * This file is part of web-stack
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\WebStack\UserInterface\Console;
13
14
use Slick\ModuleApi\Infrastructure\SlickModuleInterface;
15
use Slick\WebStack\Infrastructure\DependencyContainerFactory;
16
use Symfony\Component\Console\Attribute\AsCommand;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\Console\Style\SymfonyStyle;
23
24
/**
25
 * DisableModuleCommand
26
 *
27
 * @package Slick\WebStack\Infrastructure
28
 */
29
#[AsCommand(
30
    name: "modules:disable",
31
    description: "Disables a module",
32
    aliases: ["disable"],
33
)]
34
final class DisableModuleCommand extends DescribeModuleCommand
35
{
36
37
    use ModuleCommandTrait;
38
39
40
    public function configure(): void
41
    {
42
        $this->addArgument('module', InputArgument::REQUIRED, 'Module name to disable');
43
        $this->addOption('purge', null, InputOption::VALUE_NONE, 'Purge all module files');
44
    }
45
46
    protected function execute(InputInterface $input, OutputInterface $output): int
47
    {
48
        $this->outputStyle = new SymfonyStyle($input, $output);
49
        $moduleName = $input->getArgument('module');
50
51
        if (!file_exists($this->moduleListFile)) {
52
            $this->outputStyle->writeln(
53
                "<comment>Module '$moduleName' is not installed. No change was made.</comment>"
54
            );
55
            return Command::FAILURE;
56
        }
57
58
        $modules = $this->retrieveInstalledModules();
59
        if (!$className = $this->checkModuleExists($moduleName, $modules)) {
60
            return Command::FAILURE;
61
        }
62
63
        $new = [];
64
        foreach ($modules as $module) {
65
            if (str_ends_with($className, $module)) {
66
                continue;
67
            }
68
            $new[] = $module;
69
        }
70
71
        file_put_contents($this->moduleListFile, $this->generateModuleConfig($new));
72
73
        /** @var SlickModuleInterface $module */
74
        $module = new $className();
75
        $module->onDisable([
76
            'container' => DependencyContainerFactory::instance()->container(),
77
            'purge' => $input->getOption('purge'),
78
        ]);
79
80
        $this->outputStyle->writeln("<info>Module '$moduleName' disabled.</info>");
81
        return Command::SUCCESS;
82
    }
83
}
84