Completed
Pull Request — master (#30)
by Anton
17:34
created

ModuleCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 11
cts 11
cp 1
rs 9.2
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/bluzman
5
 */
6
7
namespace Bluzman\Command\Generate;
8
9
use Bluzman\Generator\GeneratorException;
10
use Bluzman\Input\InputArgument;
11
use Symfony\Component\Console\Input\ArrayInput;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * Generate Module Structure
17
 *
18
 * @package  Bluzman\Command\Generate
19
 */
20
class ModuleCommand extends AbstractGenerateCommand
21
{
22
    /**
23
     * Command configuration
24
     */
25 14
    protected function configure()
26
    {
27
        $this
28
            // the name of the command (the part after "bin/bluzman")
29 14
            ->setName('generate:module')
30
            // the short description shown while running "php bin/bluzman list"
31 14
            ->setDescription('Generate a new module')
32
            // the full command description shown when running the command with
33
            // the "--help" option
34 14
            ->setHelp('This command allows you to generate a module structure')
35
        ;
36
37 14
        $this->addModuleArgument();
38
39 14
        $controller = new InputArgument(
40 14
            'controller',
41 14
            InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
42 14
            'Controller name(s)'
43
        );
44
45 14
        $this->getDefinition()->addArgument($controller);
46 14
    }
47
48
    /**
49
     * @param InputInterface $input
50
     * @param OutputInterface $output
51
     * @return void
52
     */
53 1
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        try {
56 1
            $this->write("Running <info>generate:module</info> command");
57
58 1
            $module = $input->getArgument('module');
59
60 1
            $argument = $this->getDefinition()->getArgument('module');
61 1
            $argument->validate($module);
62
63
            // create main folder and subfolders
64 1
            $this->generate($input, $output);
65
66
            // verify it
67 1
            $this->verify($input, $output);
68
69 1
            $this->write("Module <info>$module</info> has been successfully created.");
70
71
            // create controllers
72 1
            $controllers = $input->getArgument('controller') ?? [];
73
74 1
            $command = $this->getApplication()->find('generate:controller');
75
76 1
            foreach ($controllers as $controller) {
77
                $arguments = [
78
                    'command' => 'generate:controller',
79
                    'module' => $module,
80
                    'controller' => $controller
81
                ];
82
                $greetInput = new ArrayInput($arguments);
83 1
                $command->run($greetInput, $output);
84
            }
85
        } catch (\Exception $e) {
86
            $this->error("ERROR: {$e->getMessage()}");
87
        }
88 1
    }
89
90
    /**
91
     * @param InputInterface $input
92
     * @param OutputInterface $output
93
     * @return void
94
     */
95 1
    protected function generate(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $output is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
    {
97 1
        $this->addSubFolders(
98 1
            $this->getApplication()->getModulePath($input->getArgument('module')),
99
            [
100 1
                'controllers',
101
                'views'
102
            ]
103
        );
104 1
    }
105
106
    /**
107
     * @param string $path
108
     * @param string[] $subFolders
109
     */
110 1
    protected function addSubFolders($path, array $subFolders = [])
111
    {
112 1
        if (!$this->getFs()->exists($path)) {
113 1
            $this->getFs()->mkdir($path);
114
        }
115
116 1
        foreach ($subFolders as $subFolderName) {
117 1
            $subFolderPath = $path . DIRECTORY_SEPARATOR . $subFolderName;
118 1
            if ($this->getFs()->exists($subFolderPath)) {
119
                $this->comment("Directory <info>$subFolderPath</info> already exists");
120
            } else {
121 1
                $this->getFs()->mkdir($subFolderPath, 0755);
122 1
                $this->getFs()->touch([$subFolderPath . DIRECTORY_SEPARATOR . '.keep']);
123
            }
124
        }
125 1
    }
126
127
    /**
128
     * @param InputInterface $input
129
     * @param OutputInterface $output
130
     * @return void
131
     * @throws GeneratorException
132
     */
133 1 View Code Duplication
    public function verify(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
134
    {
135 1
        $modulePath = $this->getApplication()->getModulePath($input->getArgument('module'));
136
137
        $paths = [
138 1
            $modulePath,
139 1
            $modulePath . DS . 'controllers',
140 1
            $modulePath . DS . 'views'
141
        ];
142
143 1
        foreach ($paths as $path) {
144 1
            if (!$this->getFs()->exists($path)) {
145
                throw new GeneratorException("Directory `$path` is not exists");
146
            }
147
        }
148 1
    }
149
}
150