Passed
Push — master ( 6e1659...b44661 )
by Anton
01:26
created

ModelCommand::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\InputException;
10
use Bluzman\Generator;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * ModelCommand
16
 *
17
 * @package  Bluzman\Command\Generate
18
 */
19
class ModelCommand extends AbstractGenerateCommand
20
{
21
    /**
22
     * Command configuration
23
     */
24 14
    protected function configure()
25
    {
26
        $this
27
            // the name of the command (the part after "bin/bluzman")
28 14
            ->setName('generate:model')
29
            // the short description shown while running "php bin/bluzman list"
30 14
            ->setDescription('Generate a new model')
31
            // the full command description shown when running the command with
32
            // the "--help" option
33 14
            ->setHelp('This command allows you to generate models files')
34
        ;
35
36 14
        $this->addModelArgument();
37 14
        $this->addTableArgument();
38 14
        $this->addForceOption();
39 14
    }
40
41
    /**
42
     * @param InputInterface $input
43
     * @param OutputInterface $output
44
     * @return void
45
     */
46 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...
47
    {
48 2
        $this->write('Running <info>generate:model</info> command');
49
        try {
50
            // validate
51 2
            $this->validateModelArgument();
52 1
            $this->validateTableArgument();
53
54
            // generate directories and files
55 1
            $this->generate($input, $output);
56
57
            // verify it
58 1
            $this->verify($input, $output);
59 1
        } catch (InputException $e) {
60 1
            $this->error("ERROR: {$e->getMessage()}");
61
        }
62 2
    }
63
64
    /**
65
     * @param InputInterface $input
66
     * @param OutputInterface $output
67
     * @return void
68
     * @throws InputException
69
     */
70 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...
71
    {
72 1
        $model = ucfirst($input->getArgument('model'));
73 1
        $table = $input->getArgument('table');
74
75
        $data = [
76 1
            'model' => $model,
77 1
            'table' => $table,
78 1
            'primaryKey' => $this->getPrimaryKey($table),
79 1
            'columns' => $this->getColumns($table)
80
        ];
81
82
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
        if ($this->getApplication()->isModelExists($modelName)) {
84
            $helper = $this->getHelperSet()->get("question");
85
            $question = new ConfirmationQuestion(
86
                "\n<question>Model $modelName would be overwritten. y/N?</question>:\n> ",
87
                false
88
            );
89
90
            if (!$helper->ask($input, $output, $question)) {
91
                return;
92
            }
93
        }
94
        */
95
        // generate table
96 1
        $tableFile = $this->getApplication()->getModelPath($model) . DS . 'Table.php';
97 1
        $this->generateFile('TableTemplate', $tableFile, $data);
98
99
        // generate row
100 1
        $rowFile = $this->getApplication()->getModelPath($model) . DS . 'Row.php';
101 1
        $this->generateFile('RowTemplate', $rowFile, $data);
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
        $modelPath = $this->getApplication()->getModelPath($model);
114
115
        $paths = [
116 1
            $modelPath . DS . 'Table.php',
117 1
            $modelPath . DS . 'Row.php'
118
        ];
119
120 1
        foreach ($paths as $path) {
121 1
            if (!$this->getFs()->exists($path)) {
122
                throw new Generator\GeneratorException("File `$path` is not exists");
123
            }
124
        }
125
126 1
        $this->write(" |> Model <info>{$model}</info> has been successfully created.");
127 1
    }
128
}
129