Passed
Push — master ( 963d3f...e19a8b )
by Anton
03:38
created

ScaffoldCommand::runGenerateModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.8333
c 0
b 0
f 0
cc 1
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 Symfony\Component\Console\Input\ArrayInput;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * Generate Module Structure
16
 *
17
 * @package  Bluzman\Command\Generate
18
 */
19
class ScaffoldCommand extends AbstractGenerateCommand
20
{
21
    /**
22
     * Command configuration
23
     */
24 15
    protected function configure()
25
    {
26
        $this
27
            // the name of the command (the part after "bin/bluzman")
28 15
            ->setName('generate:scaffold')
29
            // the short description shown while running "php bin/bluzman list"
30 15
            ->setDescription('Generate a new model and module with crud and grid')
31
            // the full command description shown when running the command with
32
            // the "--help" option
33 15
            ->setHelp('This command allows you to generate a scaffolding')
34
        ;
35
36 15
        $this->addModelArgument();
37 15
        $this->addTableArgument();
38 15
        $this->addModuleArgument();
39 15
        $this->addForceOption();
40 15
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    protected function execute(InputInterface $input, OutputInterface $output) : void
46
    {
47 1
        $this->write('Running <info>generate:scaffold</info> command');
48
        try {
49
            // generate
50 1
            $this->runGenerateModel();
51 1
            $this->runGenerateModule();
52 1
            $this->runGenerateCrud();
53 1
            $this->runGenerateGrid();
54
55
            // verify it
56 1
            $this->verify($input, $output);
57 1
        } catch (\Exception $e) {
58 1
            $this->error("ERROR: {$e->getMessage()}");
59
        }
60 1
    }
61
62
    /**
63
     * Generate Model
64
     *
65
     * @return void
66
     * @throws \Symfony\Component\Console\Exception\ExceptionInterface
67
     */
68 1 View Code Duplication
    protected function runGenerateModel() : void
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...
69
    {
70 1
        $command = $this->getApplication()->find('generate:model');
71
72
        $arguments = [
73 1
            'command' => 'generate:model',
74 1
            'model' => $this->getInput()->getArgument('model'),
75 1
            'table' => $this->getInput()->getArgument('table')
76
        ];
77 1
        $command->run(
78 1
            new ArrayInput($arguments),
79 1
            $this->getOutput()
80
        );
81 1
    }
82
83
    /**
84
     * Generate Module
85
     *
86
     * @return void
87
     * @throws \Symfony\Component\Console\Exception\ExceptionInterface
88
     */
89 1
    protected function runGenerateModule() : void
90
    {
91 1
        $command = $this->getApplication()->find('generate:module');
92
93
        $arguments = [
94 1
            'command' => 'generate:module',
95 1
            'module' => $this->getInput()->getArgument('module')
96
        ];
97 1
        $command->run(
98 1
            new ArrayInput($arguments),
99 1
            $this->getOutput()
100
        );
101 1
    }
102
103
    /**
104
     * Generate Crud
105
     *
106
     * @return void
107
     * @throws \Symfony\Component\Console\Exception\ExceptionInterface
108
     */
109 1 View Code Duplication
    protected function runGenerateCrud() : void
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...
110
    {
111 1
        $command = $this->getApplication()->find('generate:crud');
112
113
        $arguments = [
114 1
            'command' => 'generate:crud',
115 1
            'model' => $this->getInput()->getArgument('model'),
116 1
            'module' => $this->getInput()->getArgument('module')
117
        ];
118 1
        $command->run(
119 1
            new ArrayInput($arguments),
120 1
            $this->getOutput()
121
        );
122 1
    }
123
124
    /**
125
     * Generate Grid
126
     *
127
     * @return void
128
     * @throws \Symfony\Component\Console\Exception\ExceptionInterface
129
     */
130 1 View Code Duplication
    protected function runGenerateGrid() : void
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...
131
    {
132 1
        $command = $this->getApplication()->find('generate:grid');
133
134
        $arguments = [
135 1
            'command' => 'generate:grid',
136 1
            'model' => $this->getInput()->getArgument('model'),
137 1
            'module' => $this->getInput()->getArgument('module')
138
        ];
139 1
        $command->run(
140 1
            new ArrayInput($arguments),
141 1
            $this->getOutput()
142
        );
143 1
    }
144
145
    /**
146
     * {@inheritdoc}
147
     *
148
     * @throws GeneratorException
149
     */
150 1
    public function verify(InputInterface $input, OutputInterface $output) : void
151
    {
152 1
        $model = $input->getArgument('model');
153 1
        $module = $input->getArgument('module');
154
155 1
        $modelPath = $this->getApplication()->getModelPath($model);
0 ignored issues
show
Bug introduced by
It seems like $model defined by $input->getArgument('model') on line 152 can also be of type array<integer,string> or null; however, Bluzman\Application\Application::getModelPath() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
156 1
        $modulePath = $this->getApplication()->getModulePath($module);
0 ignored issues
show
Bug introduced by
It seems like $module defined by $input->getArgument('module') on line 153 can also be of type array<integer,string> or null; however, Bluzman\Application\Application::getModulePath() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
157
158
        $paths = [
159 1
            $modelPath . DS . 'Crud.php',
160 1
            $modelPath . DS . 'Grid.php',
161 1
            $modelPath . DS . 'Row.php',
162 1
            $modelPath . DS . 'Table.php',
163 1
            $modulePath . DS . 'controllers' . DS . 'crud.php',
164 1
            $modulePath . DS . 'controllers' . DS . 'grid.php',
165 1
            $modulePath . DS . 'views' . DS . 'crud.phtml',
166 1
            $modulePath . DS . 'views' . DS . 'grid.phtml',
167
        ];
168
169 1
        foreach ($paths as $path) {
170 1
            if (!$this->getFs()->exists($path)) {
171 1
                throw new GeneratorException("File `$path` is not exists");
172
            }
173
        }
174
175
        $this->write("Scaffolding for <info>{$model}</info> has been successfully created.");
176
    }
177
}
178