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

AbstractGenerateCommand::validateModelArgument()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

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