Completed
Pull Request — master (#35)
by Anton
13:23
created

CrudCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 122
Duplicated Lines 34.43 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 70.59%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 2
cbo 7
dl 42
loc 122
ccs 36
cts 51
cp 0.7059
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 14 1
A execute() 17 17 2
B generate() 0 38 3
B verify() 25 25 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 class
83 1
        $this->write(" |> Generate CRUD class <info>$model\\Crud</info>");
84 1
        $crudFile = $this->getApplication()->getModelPath($model) . DS . 'Crud.php';
85 1
        $this->generateFile('CrudTemplate', $crudFile, $data);
86
87 1
        if ($module) {
88
            if (!$this->getApplication()->isModelExists($model)) {
89
                throw new InputException(
90
                    "Model $model is not exist, " .
91
                    "run command <question>bluzman generate:model $model</question> before"
92
                );
93
            }
94
95
            $this->write(" |> Generate CRUD controller <info>$module/controllers/crud.php</info>");
96
97
            $controllerFile = $this->getControllerPath($module, 'crud');
98
            $this->generateFile('CrudControllerTemplate', $controllerFile, $data);
99
100
            $this->write(" |> Generate CRUD view <info>$module/views/crud.phtml</info>");
101
102
            $tableInstance = $this->getTableInstance($model);
103
            $data['columns'] = $tableInstance::getMeta();
104
105
            $viewFile = $this->getViewPath($module, 'crud');
106
            $this->generateFile('CrudViewTemplate', $viewFile, $data);
107
        }
108 1
    }
109
110
    /**
111
     * @param InputInterface $input
112
     * @param OutputInterface $output
113
     * @return void
114
     * @throws \Bluzman\Generator\GeneratorException
115
     */
116 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...
117
    {
118 1
        $model = $input->getArgument('model');
119 1
        $module = $input->getArgument('module');
120
121 1
        $modelPath = $this->getApplication()->getModelPath($model);
122
123
        $paths = [
124 1
            $modelPath . DS . 'Crud.php',
125
        ];
126
127 1
        foreach ($paths as $path) {
128 1
            if (!$this->getFs()->exists($path)) {
129
                throw new Generator\GeneratorException("File `$path` is not exists");
130
            }
131
        }
132
133 1
        $this->write(" |> CRUD for <info>{$model}</info> has been successfully created.");
134 1
        if ($module) {
135
            $this->write(
136
                " |> <options=bold>Open page <info>/acl</info> in your browser " .
137
                "and set permission <info>Management</info> for <info>{$module}</info> module</>"
138
            );
139
        }
140 1
    }
141
}
142