EnableModuleCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 26
c 2
b 0
f 0
dl 0
loc 57
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 20 2
A configure() 0 3 1
A __construct() 0 5 1
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\Output\OutputInterface;
21
use Symfony\Component\Console\Style\SymfonyStyle;
22
23
/**
24
 * EnableModuleCommand
25
 *
26
 * @package Slick\WebStack\Infrastructure
27
 */
28
#[AsCommand(
29
    name: "modules:enable",
30
    description: "Enables a modules.",
31
    aliases: ["enable"]
32
)]
33
class EnableModuleCommand extends Command
34
{
35
    use ModuleCommandTrait;
36
37
    /** @var string */
38
    public const ENABLED_MODULES_FILE = '/config/modules/enabled.php';
39
40
    protected string $appRoot;
41
42
    /** @var string  */
43
    protected string $moduleListFile;
44
45
    protected ?SymfonyStyle $outputStyle = null;
46
47
    /**
48
     * Creates a EnableModuleCommand
49
     *
50
     * @param string $appRoot
51
     */
52
    public function __construct(string $appRoot)
53
    {
54
        parent::__construct();
55
        $this->appRoot = $appRoot;
56
        $this->moduleListFile = $this->appRoot . self::ENABLED_MODULES_FILE;
57
    }
58
59
    public function configure(): void
60
    {
61
        $this->addArgument('module', InputArgument::REQUIRED, "Module name to enable");
62
    }
63
64
65
    protected function execute(InputInterface $input, OutputInterface $output): int
66
    {
67
        $this->outputStyle = new SymfonyStyle($input, $output);
68
        $moduleName = $input->getArgument('module');
69
        $this->checkConfigFile();
70
71
        $modules = $this->retrieveInstalledModules();
72
        if (!$retrieveModuleName = $this->checkModuleNotExists($moduleName, $modules)) {
73
            return Command::FAILURE;
74
        }
75
76
        $modules[] = $retrieveModuleName;
77
        file_put_contents($this->moduleListFile, $this->generateModuleConfig($modules));
78
79
        /** @var SlickModuleInterface $module */
80
        $module = new $retrieveModuleName();
81
        $module->onEnable(['container' => DependencyContainerFactory::instance()->container()]);
82
83
        $this->outputStyle?->writeln("<info>Module '$moduleName' enabled.</info>");
84
        return Command::SUCCESS;
85
    }
86
}
87