Issues (108)

src/Command/Generate/ModelCommand.php (4 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * @copyright Bluz PHP Team
5
 * @link https://github.com/bluzphp/bluzman
6
 */
7
8
namespace Bluzman\Command\Generate;
9
10
use Bluzman\Input\InputException;
11
use Bluzman\Generator;
12
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
/**
16
 * ModelCommand
17
 *
18
 * @package  Bluzman\Command\Generate
19
 */
20
class ModelCommand extends AbstractGenerateCommand
21
{
22
    /**
23
     * Command configuration
24 15
     */
25
    protected function configure()
26
    {
27
        $this
28 15
            // the name of the command (the part after "bin/bluzman")
29
            ->setName('generate:model')
30 15
            // the short description shown while running "php bin/bluzman list"
31
            ->setDescription('Generate a new model')
32
            // the full command description shown when running the command with
33 15
            // the "--help" option
34
            ->setHelp('This command allows you to generate models files')
35
        ;
36 15
37 15
        $this->addModelArgument();
38 15
        $this->addTableArgument();
39 15
        $this->addForceOption();
40
    }
41
42
    /**
43
     * @param InputInterface $input
44
     * @param OutputInterface $output
45
     * @return int
46 3
     * @throws Generator\GeneratorException
47
     */
48 3
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50
        $this->write('Running <info>generate:model</info> command');
51 3
        try {
52 1
            // validate
53
            $this->validateModelArgument();
54
            $this->validateTableArgument();
55 1
56
            // generate directories and files
57
            $this->generate($input, $output);
58 1
59 2
            // verify it
60 2
            $this->verify($input, $output);
61
            return 0;
62 3
        } catch (InputException $e) {
63
            $this->error("ERROR: {$e->getMessage()}");
64
            return $e->getCode();
65
        }
66
    }
67
68
    /**
69
     * @param InputInterface $input
70 1
     * @param OutputInterface $output
71
     * @return void
72 1
     * @throws InputException
73 1
     */
74
    protected function generate(InputInterface $input, OutputInterface $output): void
75
    {
76 1
        $model = ucfirst($input->getArgument('model'));
77 1
        $table = $input->getArgument('table');
78 1
79 1
        $data = [
80
            'model' => $model,
81
            'table' => $table,
82
            'primaryKey' => $this->getPrimaryKey($table),
83
            'columns' => $this->getColumns($table)
84
        ];
85
86
        /*
87
        if ($this->getApplication()->isModelExists($modelName)) {
88
            $helper = $this->getHelperSet()->get("question");
89
            $question = new ConfirmationQuestion(
90
                "\n<question>Model $modelName would be overwritten. y/N?</question>:\n> ",
91
                false
92
            );
93
94
            if (!$helper->ask($input, $output, $question)) {
95
                return;
96 1
            }
97 1
        }
98
        */
99
        // generate table
100 1
        $tableFile = $this->getApplication()->getModelPath($model) . DS . 'Table.php';
0 ignored issues
show
The constant Bluzman\Command\Generate\DS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
101 1
        $this->generateFile('TableTemplate', $tableFile, $data);
102 1
103
        // generate row
104
        $rowFile = $this->getApplication()->getModelPath($model) . DS . 'Row.php';
105
        $this->generateFile('RowTemplate', $rowFile, $data);
106
    }
107
108
    /**
109
     * @param InputInterface $input
110 1
     * @param OutputInterface $output
111
     * @return void
112 1
     * @throws \Bluzman\Generator\GeneratorException
113 1
     */
114
    public function verify(InputInterface $input, OutputInterface $output): void
115
    {
116 1
        $model = $input->getArgument('model');
117 1
        $modelPath = $this->getApplication()->getModelPath($model);
118
119
        $paths = [
120 1
            $modelPath . DS . 'Table.php',
0 ignored issues
show
The constant Bluzman\Command\Generate\DS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
121 1
            $modelPath . DS . 'Row.php'
122 1
        ];
123
124
        foreach ($paths as $path) {
125
            if (!$this->getFs()->exists($path)) {
126 1
                throw new Generator\GeneratorException("File `$path` is not exists");
127 1
            }
128
        }
129
130
        $this->write(" |> Model <info>{$model}</info> has been successfully created.");
131
    }
132
}
133