Passed
Push — master ( 6e1659...b44661 )
by Anton
01:26
created

ScaffoldCommand::runGenerateModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
crap 2
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 14
    protected function configure()
25
    {
26
        $this
27
            // the name of the command (the part after "bin/bluzman")
28 14
            ->setName('generate:scaffold')
29
            // the short description shown while running "php bin/bluzman list"
30 14
            ->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 14
            ->setHelp('This command allows you to generate a scaffolding')
34
        ;
35
36 14
        $this->addModelArgument();
37 14
        $this->addTableArgument();
38 14
        $this->addModuleArgument();
39 14
        $this->addForceOption();
40 14
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected function execute(InputInterface $input, OutputInterface $output) : void
46
    {
47
        $this->write('Running <info>generate:scaffold</info> command');
48
        try {
49
            // generate
50
            $this->runGenerateModel();
51
            $this->runGenerateModule();
52
            $this->runGenerateCrud();
53
            $this->runGenerateGrid();
54
55
            // verify it
56
            $this->verify($input, $output);
57
        } catch (\Exception $e) {
58
            $this->error("ERROR: {$e->getMessage()}");
59
        }
60
    }
61
62
    /**
63
     * Generate Model
64
     *
65
     * @return void
66
     */
67 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...
68
    {
69
        $command = $this->getApplication()->find('generate:model');
70
71
        $arguments = [
72
            'command' => 'generate:model',
73
            'model' => $this->getInput()->getArgument('model'),
74
            'table' => $this->getInput()->getArgument('table')
75
        ];
76
        $command->run(
77
            new ArrayInput($arguments),
78
            $this->getOutput()
79
        );
80
    }
81
82
    /**
83
     * Generate Module
84
     *
85
     * @return void
86
     */
87
    protected function runGenerateModule() : void
88
    {
89
        $command = $this->getApplication()->find('generate:module');
90
91
        $arguments = [
92
            'command' => 'generate:module',
93
            'module' => $this->getInput()->getArgument('module')
94
        ];
95
        $command->run(
96
            new ArrayInput($arguments),
97
            $this->getOutput()
98
        );
99
    }
100
101
    /**
102
     * Generate Crud
103
     *
104
     * @return void
105
     */
106 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...
107
    {
108
        $command = $this->getApplication()->find('generate:crud');
109
110
        $arguments = [
111
            'command' => 'generate:crud',
112
            'model' => $this->getInput()->getArgument('model'),
113
            'module' => $this->getInput()->getArgument('module')
114
        ];
115
        $command->run(
116
            new ArrayInput($arguments),
117
            $this->getOutput()
118
        );
119
    }
120
121
    /**
122
     * Generate Grid
123
     *
124
     * @return void
125
     */
126 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...
127
    {
128
        $command = $this->getApplication()->find('generate:grid');
129
130
        $arguments = [
131
            'command' => 'generate:grid',
132
            'model' => $this->getInput()->getArgument('model'),
133
            'module' => $this->getInput()->getArgument('module')
134
        ];
135
        $command->run(
136
            new ArrayInput($arguments),
137
            $this->getOutput()
138
        );
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     *
144
     * @throws GeneratorException
145
     */
146
    public function verify(InputInterface $input, OutputInterface $output) : void
147
    {
148
        $model = $input->getArgument('model');
149
        $module = $input->getArgument('module');
150
151
        $modelPath = $this->getApplication()->getModelPath($model);
152
        $modulePath = $this->getApplication()->getModulePath($module);
153
154
        $paths = [
155
            $modelPath . DS . 'Crud.php',
156
            $modelPath . DS . 'Grid.php',
157
            $modelPath . DS . 'Row.php',
158
            $modelPath . DS . 'Table.php',
159
            $modulePath . DS . 'controllers' . DS . 'crud.php',
160
            $modulePath . DS . 'controllers' . DS . 'grid.php',
161
            $modulePath . DS . 'views' . DS . 'crud.phtml',
162
            $modulePath . DS . 'views' . DS . 'grid.phtml',
163
        ];
164
165
        foreach ($paths as $path) {
166
            if (!$this->getFs()->exists($path)) {
167
                throw new GeneratorException("File `$path` is not exists");
168
            }
169
        }
170
171
        $this->write("Scaffolding for <info>{$model}</info> has been successfully created.");
172
    }
173
}
174