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

GridCommand::generate()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.5293

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 35
ccs 11
cts 18
cp 0.6111
rs 8.8571
cc 3
eloc 18
nc 4
nop 2
crap 3.5293
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
    }
40
41
    /**
42
     * @param InputInterface  $input
43
     * @param OutputInterface $output
44
     *
45
     * @return void
46
     * @throws \Bluzman\Generator\GeneratorException
47
     */
48 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...
49
    {
50 2
        $this->write('Running <info>generate:grid</info> command');
51
        try {
52
            // validate
53 2
            $this->validateModelArgument();
54 1
            $this->validateModuleArgument();
55
56
            // generate directories and files
57 1
            $this->generate($input, $output);
58
59
            // verify files
60 1
            $this->verify($input, $output);
61 1
        } catch (InputException $e) {
62 1
            $this->error("ERROR: {$e->getMessage()}");
63
        }
64 2
    }
65
66
    /**
67
     * @param InputInterface $input
68
     * @param OutputInterface $output
69
     * @return void
70
     * @throws InputException
71
     */
72 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...
73
    {
74 1
        $model = ucfirst($input->getArgument('model'));
75 1
        $module = $input->getArgument('module');
76
77
        // template data
78
        $data = [
79 1
            'model' => $model,
80 1
            'module' => $module
81
        ];
82
83 1
        if ($module) {
84
            // controller and view generators required the `Model\Table` class
85
            // validator is present on previous step
86
            $data['columns'] = $this->getTableInstance($model)::getMeta();
87
        }
88
89
        // generate GRID class
90 1
        $this->write(" |> Generate Grid class <info>$model\\Grid</info>");
91
92 1
        $gridFile = $this->getApplication()->getModelPath($model) . DS . 'Grid.php';
93 1
        $this->generateFile('GridTemplate', $gridFile, $data);
94
95 1
        if ($module) {
96
            $this->write(" |> Generate Grid controller <info>$module/controllers/grid.php</info>");
97
98
            $controllerFile = $this->getControllerPath($module, 'grid');
99
            $this->generateFile('GridControllerTemplate', $controllerFile, $data);
100
101
            $this->write(" |> Generate Grid view <info>$module/views/grid.phtml</info>");
102
103
            $viewFile = $this->getViewPath($module, 'grid');
104
            $this->generateFile('GridViewTemplate', $viewFile, $data);
105
        }
106 1
    }
107
108
    /**
109
     * @param InputInterface $input
110
     * @param OutputInterface $output
111
     * @return void
112
     * @throws \Bluzman\Generator\GeneratorException
113
     */
114 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...
115
    {
116 1
        $model = $input->getArgument('model');
117 1
        $module = $input->getArgument('module');
118
119 1
        $modelPath = $this->getApplication()->getModelPath($model);
120
121
        $paths = [
122 1
            $modelPath . DS . 'Grid.php',
123
        ];
124
125 1
        foreach ($paths as $path) {
126 1
            if (!$this->getFs()->exists($path)) {
127
                throw new Generator\GeneratorException("File `$path` is not exists");
128
            }
129
        }
130
131
        // notifications
132 1
        $this->write(" |> GRID for <info>{$model}</info> has been successfully created.");
133
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