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

GridCommand::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 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
        // generate GRID class
84 1
        $gridFile = $this->getApplication()->getModelPath($model) . DS . 'Grid.php';
85 1
        $this->generateFile('GridTemplate', $gridFile, $data);
86
87 1
        if ($module) {
88
            $this->write(" |> Generate Grid controller <info>$module/controllers/grid.php</info>");
89
90
            // controller and view generators required the `Model\Table` class
91
            // validator is present on previous step
92
            $data['columns'] = $this->getTableInstance($model)::getMeta();
93
94
            $controllerFile = $this->getControllerPath($module, 'grid');
95
            $this->generateFile('GridControllerTemplate', $controllerFile, $data);
96
97
            $this->write(" |> Generate Grid view <info>$module/views/grid.phtml</info>");
98
99
            $viewFile = $this->getViewPath($module, 'grid');
100
            $this->generateFile('GridViewTemplate', $viewFile, $data);
101
        }
102 1
    }
103
104
    /**
105
     * @param InputInterface $input
106
     * @param OutputInterface $output
107
     * @return void
108
     * @throws \Bluzman\Generator\GeneratorException
109
     */
110 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...
111
    {
112 1
        $model = $input->getArgument('model');
113 1
        $module = $input->getArgument('module');
114
115 1
        $modelPath = $this->getApplication()->getModelPath($model);
116
117
        $paths = [
118 1
            $modelPath . DS . 'Grid.php',
119
        ];
120
121 1
        foreach ($paths as $path) {
122 1
            if (!$this->getFs()->exists($path)) {
123
                throw new Generator\GeneratorException("File `$path` is not exists");
124
            }
125
        }
126
127
        // notifications
128 1
        $this->write(" |> GRID for <info>{$model}</info> has been successfully created.");
129
130 1
        if ($module) {
131
            $this->write(
132
                " |> <options=bold>Open page <info>/acl</info> in your browser ".
133
                "and set permission <info>Management</info> for <info>{$module}</info> module</>"
134
            );
135
        }
136 1
    }
137
}
138