Passed
Pull Request — master (#35)
by Anton
03:28
created

CrudCommand::execute()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 17
loc 17
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 5
nop 2
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\Input\InputArgument;
10
use Bluzman\Input\InputException;
11
use Bluzman\Generator;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * ModelCommand
17
 *
18
 * @package  Bluzman\Command\Generate
19
 */
20
class CrudCommand 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:crud')
30
            // the short description shown while running "php bin/bluzman list"
31 14
            ->setDescription('Generate a CRUD for model')
32
            // the full command description shown when running the command with
33
            // the "--help" option
34 14
            ->setHelp('This command allows you to generate CRUD files');
35
36 14
        $this->addModelArgument();
37 14
        $this->addModuleArgument(InputArgument::OPTIONAL);
38 14
    }
39
40
    /**
41
     * @param InputInterface  $input
42
     * @param OutputInterface $output
43
     *
44
     * @return void
45
     * @throws \Bluzman\Generator\GeneratorException
46
     */
47 3 View Code Duplication
    protected function execute(InputInterface $input, OutputInterface $output) : 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...
48
    {
49 3
        $this->write('Running <info>generate:crud</info> command');
50
        try {
51
            // validate
52 3
            $this->validateModelArgument();
53 1
            $this->validateModuleArgument();
54
55
            // generate directories and files
56 1
            $this->generate($input, $output);
57
58
            // verify it
59 1
            $this->verify($input, $output);
60 2
        } catch (InputException $e) {
61 2
            $this->error("ERROR: {$e->getMessage()}");
62
        }
63 3
    }
64
65
    /**
66
     * @param InputInterface $input
67
     * @param OutputInterface $output
68
     * @return void
69
     * @throws InputException
70
     */
71 1
    protected function generate(InputInterface $input, OutputInterface $output) : void
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...
72
    {
73 1
        $model = ucfirst($input->getArgument('model'));
74 1
        $module = $input->getArgument('module');
75
76
        // template data
77
        $data = [
78 1
            'model' => $model,
79 1
            'module' => $module
80
        ];
81
82
        // generate CRUD
83 1
        $crudFile = $this->getApplication()->getModelPath($model) . DS . 'Crud.php';
84 1
        $this->generateFile('CrudTemplate', $crudFile, $data);
85
86 1
        if ($module) {
87
            if (!$this->getApplication()->isModelExists($model)) {
88
                throw new InputException(
89
                    "Model $model is not exist, " .
90
                    "run command <question>bluzman generate:model $model</question> before"
91
                );
92
            }
93
94
            $this->write(" |> Generate CRUD controller <info>$module/controllers/crud.php</info>");
95
96
            $controllerFile = $this->getControllerPath($module, 'crud');
97
            $this->generateFile('CrudControllerTemplate', $controllerFile, $data);
98
99
            $this->write(" |> Generate CRUD view <info>$module/views/crud.phtml</info>");
100
101
            $tableInstance = $this->getTableInstance($model);
102
            $data['columns'] = $tableInstance::getMeta();
103
104
            $viewFile = $this->getViewPath($module, 'crud');
105
            $this->generateFile('CrudViewTemplate', $viewFile, $data);
106
        }
107 1
    }
108
109
    /**
110
     * @param InputInterface $input
111
     * @param OutputInterface $output
112
     * @return void
113
     * @throws \Bluzman\Generator\GeneratorException
114
     */
115 1 View Code Duplication
    public function verify(InputInterface $input, OutputInterface $output) : 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...
116
    {
117 1
        $model = $input->getArgument('model');
118 1
        $module = $input->getArgument('module');
119
120 1
        $modelPath = $this->getApplication()->getModelPath($model);
121
122
        $paths = [
123 1
            $modelPath . DS . 'Crud.php',
124
        ];
125
126 1
        foreach ($paths as $path) {
127 1
            if (!$this->getFs()->exists($path)) {
128
                throw new Generator\GeneratorException("File `$path` is not exists");
129
            }
130
        }
131
132 1
        $this->write(" |> CRUD for <info>{$model}</info> has been successfully created.");
133 1
        if ($module) {
134
            $this->write(
135
                " |> <options=bold>Open page <info>/acl</info> in your browser ".
136
                "and set permission <info>Management</info> for <info>{$module}</info> module</>"
137
            );
138
        }
139 1
    }
140
}
141