Completed
Pull Request — master (#38)
by Anton
12:38
created

GridCommand::execute()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 17

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.7
c 0
b 0
f 0
cc 2
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 GridCommand 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:grid')
30
            // the short description shown while running "php bin/bluzman list"
31 14
            ->setDescription('Generate a GRID 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 GRID files')
35
        ;
36
37 14
        $this->addModelArgument();
38 14
        $this->addModuleArgument(InputArgument::OPTIONAL);
39 14
        $this->addForceOption();
40 14
    }
41
42
    /**
43
     * @param InputInterface  $input
44
     * @param OutputInterface $output
45
     *
46
     * @return void
47
     * @throws \Bluzman\Generator\GeneratorException
48
     */
49 2 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...
50
    {
51 2
        $this->write('Running <info>generate:grid</info> command');
52
        try {
53
            // validate
54 2
            $this->validateModelArgument();
55 1
            $this->validateModuleArgument();
56
57
            // generate directories and files
58 1
            $this->generate($input, $output);
59
60
            // verify files
61 1
            $this->verify($input, $output);
62 1
        } catch (InputException $e) {
63 1
            $this->error("ERROR: {$e->getMessage()}");
64
        }
65 2
    }
66
67
    /**
68
     * @param InputInterface  $input
69
     * @param OutputInterface $output
70
     *
71
     * @return void
72
     * @throws Generator\GeneratorException
73
     */
74 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...
75
    {
76 1
        $model = ucfirst($input->getArgument('model'));
77 1
        $module = $input->getArgument('module');
78
79
        // template data
80
        $data = [
81 1
            'model' => $model,
82 1
            'module' => $module
83
        ];
84
85 1
        if ($module) {
86
            // controller and view generators required the `Model\Table` class
87
            // validator is present on previous step
88
            $data['columns'] = $this->getTableInstance($model)::getMeta();
89
        }
90
91
        // generate GRID class
92 1
        $this->write(" |> Generate Grid class <info>$model\\Grid</info>");
93
94 1
        $gridFile = $this->getApplication()->getModelPath($model) . DS . 'Grid.php';
95 1
        $this->generateFile('GridTemplate', $gridFile, $data);
96
97 1
        if ($module) {
98
            $this->write(" |> Generate Grid controller <info>$module/controllers/grid.php</info>");
99
100
            $controllerFile = $this->getControllerPath($module, 'grid');
101
            $this->generateFile('GridControllerTemplate', $controllerFile, $data);
102
103
            $this->write(" |> Generate Grid view <info>$module/views/grid.phtml</info>");
104
105
            $viewFile = $this->getViewPath($module, 'grid');
106
            $this->generateFile('GridViewTemplate', $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);
0 ignored issues
show
Bug introduced by
It seems like $model defined by $input->getArgument('model') on line 118 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...
122
123
        $paths = [
124 1
            $modelPath . DS . 'Grid.php',
125
        ];
126
127 1
        foreach ($paths as $path) {
128 1
            if (!$this->getFs()->exists($path)) {
129 1
                throw new Generator\GeneratorException("File `$path` is not exists");
130
            }
131
        }
132
133
        // notifications
134 1
        $this->write(" |> GRID for <info>{$model}</info> has been successfully created.");
135
136 1
        if ($module) {
137
            $this->write(
138
                " |> <options=bold>Open page <info>/acl</info> in your browser " .
139
                "and set permission <info>Management</info> for <info>{$module}</info> module</>"
140
            );
141
        }
142 1
    }
143
}
144