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

AbstractGenerateCommand::addForceOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
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 Bluz\Db\Table;
10
use Bluz\Proxy\Db;
11
use Bluz\Validator\Validator;
12
use Bluzman\Command\AbstractCommand;
13
use Bluzman\Generator\Generator;
14
use Bluzman\Generator\GeneratorException;
15
use Bluzman\Input\InputArgument;
16
use Bluzman\Input\InputException;
17
use Bluzman\Input\InputOption;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
/**
22
 * AbstractCommand
23
 *
24
 * @package  Bluzman\Command\Generate
25
 */
26
abstract class AbstractGenerateCommand extends AbstractCommand
27
{
28
    /**
29
     * @param InputInterface $input
30
     * @param OutputInterface $output
31
     * @return mixed
32
     */
33
    abstract public function verify(InputInterface $input, OutputInterface $output) : void;
34
35
    /**
36
     * Add Force Option
37
     *
38
     * @return void
39
     */
40 14
    protected function addForceOption() : void
41
    {
42 14
        $force = new InputOption('--force', '-f', InputOption::VALUE_NONE, 'Rewrite previously generated files');
43 14
        $this->getDefinition()->addOption($force);
44 14
    }
45
46
    /**
47
     * Add Model Argument
48
     *
49
     * @return void
50
     */
51 14
    protected function addModelArgument() : void
52
    {
53 14
        $model = new InputArgument('model', InputArgument::REQUIRED, 'Model name is required');
54 14
        $this->getDefinition()->addArgument($model);
55 14
    }
56
57
    /**
58
     * Validate Model Argument
59
     *
60
     * @return void
61
     * @throws \Bluzman\Input\InputException
62
     */
63 8
    protected function validateModelArgument() : void
64
    {
65 8
        $model = $this->getInput()->getArgument('model');
66
67 8
        $validator = Validator::create()
68 8
            ->string()
69 8
            ->alphaNumeric()
70 8
            ->noWhitespace();
71
72 8
        if ($this->getDefinition()->getArgument('model')->isRequired()
73 8
            && !$validator->validate($model)) {
74 4
            throw new InputException($validator->getError());
75
        }
76 4
    }
77
78
    /**
79
     * Add Table Argument
80
     *
81
     * @return void
82
     */
83 14
    protected function addTableArgument() : void
84
    {
85 14
        $table = new InputArgument('table', InputArgument::REQUIRED, 'Table name is required');
86 14
        $this->getDefinition()->addArgument($table);
87 14
    }
88
89
    /**
90
     * Validate Table Argument
91
     *
92
     * @return void
93
     * @throws \Bluzman\Input\InputException
94
     */
95 1 View Code Duplication
    protected function validateTableArgument() : 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...
96
    {
97 1
        $table = $this->getInput()->getArgument('table');
98
99 1
        $validator = Validator::create()
100 1
            ->string()
101 1
            ->alphaNumeric('_')
102 1
            ->noWhitespace();
103
104 1
        if ($this->getDefinition()->getArgument('table')->isRequired()
105 1
            && !$validator->validate($table)) {
106
            throw new InputException($validator->getError());
107
        }
108 1
    }
109
110
    /**
111
     * Get Table instance
112
     *
113
     * @param  string $model
114
     *
115
     * @return Table
116
     * @throws \Bluzman\Generator\GeneratorException
117
     */
118
    protected function getTableInstance($model) : Table
119
    {
120
        $file = $this->getApplication()->getModelPath($model) . DS . 'Table.php';
121
        if (!file_exists($file)) {
122
            throw new GeneratorException(
123
                "Model $model is not exist, run command `bluzman generate:model $model` before"
124
            );
125
        }
126
        include_once $file;
127
        $class = '\\Application\\' . ucfirst($model) . '\\Table';
128
        if (!class_exists($class)) {
129
            throw new GeneratorException("Bluzman can't found `Table` class for model `$model`");
130
        }
131
        return $class::getInstance();
132
    }
133
134
    /**
135
     * Required for correct mock it
136
     *
137
     * @param  string $class
138
     * @return mixed
139
     */
140 1
    protected function getTemplate($class)
141
    {
142 1
        $class = '\\Bluzman\\Generator\\Template\\' . $class;
143 1
        return new $class;
144
    }
145
146
    /**
147
     * Small wrapper for simplify code
148
     *
149
     * @param  string $class
150
     * @param  string $file
151
     * @param  array  $data
152
     *
153
     * @return void
154
     */
155 5
    protected function generateFile($class, $file, array $data = []) : void
156
    {
157 5
        if (file_exists($file) && !$this->getInput()->getOption('force')) {
158
            $this->comment(" |> File <info>$file</info> already exists");
159
            return;
160
        }
161
162 5
        $template = $this->getTemplate($class);
163 5
        $template->setFilePath($file);
164 5
        $template->setTemplateData($data);
165
166 5
        $generator = new Generator($template);
167 5
        $generator->make();
168 5
    }
169
170
    /**
171
     * @return string
172
     */
173 2
    protected function getControllerPath($module, $controller) : string
174
    {
175 2
        return $this->getApplication()->getModulePath($module)
176 2
            . DS . 'controllers'
177 2
            . DS . $controller
178 2
            . '.php';
179
    }
180
181
    /**
182
     * @return string
183
     */
184 1
    protected function getViewPath($module, $controller) : string
185
    {
186 1
        return $this->getApplication()->getModulePath($module)
187 1
            . DS . 'views'
188 1
            . DS . $controller
189 1
            . '.phtml';
190
    }
191
192
    /**
193
     * @todo move it to DB class
194
     * @return array
195
     */
196
    protected function getPrimaryKey($table)
197
    {
198
        $connect = Db::getOption('connect');
199
200
        return Db::fetchColumn(
201
            '
202
            SELECT COLUMN_NAME
203
            FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
204
            WHERE TABLE_SCHEMA = ?
205
              AND TABLE_NAME = ?
206
             AND CONSTRAINT_NAME = ?
207
            ',
208
            [$connect['name'], $table, 'PRIMARY']
209
        );
210
    }
211
212
    /**
213
     * @todo move it to DB class
214
     * @return array
215
     */
216
    protected function getColumns($table)
217
    {
218
        $connect = Db::getOption('connect');
219
220
        return Db::fetchAll(
221
            '
222
            SELECT 
223
              COLUMN_NAME as name,
224
              COLUMN_TYPE as type
225
            FROM INFORMATION_SCHEMA.COLUMNS
226
            WHERE TABLE_SCHEMA = ?
227
              AND TABLE_NAME = ?
228
            ',
229
            [$connect['name'], $table]
230
        );
231
    }
232
}
233