Failed Conditions
Push — master ( 27ab61...b61ce7 )
by Yangsin
44:29
created

EntityFromYamlGenerator::getEntityList()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 16
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 25
rs 8.439
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * This program is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU General Public License
12
 * as published by the Free Software Foundation; either version 2
13
 * of the License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23
 */
24
25
namespace Eccube\Command\PluginCommand;
26
27
use Symfony\Component\Yaml\Yaml;
28
use Eccube\Command\PluginCommand\AbstractPluginGenerator;
29
use Eccube\Doctrine\ORM\Mapping\Driver\YamlDriver;
30
use Doctrine\DBAL\Types\Type;
31
use Doctrine\ORM\Mapping\ClassMetadataInfo;
32
use Doctrine\ORM\Tools\EntityGenerator;
33
34
class EntityFromYamlGenerator extends AbstractPluginGenerator
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
35
{
36
37
    const PLUGIN_PREFIX = 'plg_';
38
39
    /**
40
     * Entityリスト
41
     * @var array
42
     */
43
    private $entityList = null;
44
45 View Code Duplication
    protected function getHeader()
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...
46
    {
47
        $this->output->writeln('------------------------------------------------------');
48
        $this->output->writeln('---Plugin Generator for Entity');
49
        $this->output->writeln('---[*] You need to create yaml file first.');
50
        $this->output->writeln('---[*]You can exit from Console Application, by typing ' . self::STOP_PROCESS . ' instead of typing another word.');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
51
        $this->output->writeln('------------------------------------------------------');
52
        $this->output->writeln('');
53
    }
54
55
    protected function start()
56
    {
57
        $fsList = array(
58
            'dir' => array(),
59
            'file' => array(),
60
        );
61
62
        $pluginCode = $this->paramList['pluginCode']['value'];
63
        $yamlList = $this->paramList['entityList']['value'];
64
65
        $codePath = $this->app['config']['root_dir'] . '/app/Plugin/' . $pluginCode;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
66
67
        $dirList = array('Entity', 'Repository', '/Resource/doctrine/migration');
68 View Code Duplication
        foreach ($dirList as $dirName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
69
            $dirPath = $codePath . '/' . $dirName;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
70
            if (!is_dir($dirPath)) {
71
                mkdir($dirPath);
72
            }
73
            if (is_dir($dirPath)) {
74
                $fsList['dir'][$dirPath] = true;
75
            } else {
76
                $fsList['dir'][$dirPath] = false;
77
            }
78
        }
79
        $metadetas = array();
80
81
82
        $YamlDriver = new YamlDriver(array($codePath . '/Resource/doctrine'));
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
83
        foreach ($yamlList as $pathYaml => $fullClassName) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
84
85
            $MetadataInfo = new ClassMetadataInfo($fullClassName);
86
            $YamlDriver->loadMetadataForClass($fullClassName, $MetadataInfo);
87
            $metadetas[] = $MetadataInfo;
88
89
            //Entity作成
90
            $EntityGenerator = new EntityGenerator();
91
            $EntityGenerator->setBackupExisting(false);
92
            $EntityGenerator->setClassToExtend('Eccube\\Entity\\AbstractEntity');
93
            $EntityGenerator->setGenerateAnnotations(false);
94
            $EntityGenerator->setRegenerateEntityIfExists(true);
95
            $EntityGenerator->setGenerateStubMethods(true);
96
            $EntityGenerator->setUpdateEntityIfExists(false);
97
98
            $pathClass = $this->app['config']['root_dir'] . '/app/';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
99
            $EntityGenerator->generate(array($MetadataInfo), $pathClass);
100
            $pathClass .= str_replace('\\', DIRECTORY_SEPARATOR, $MetadataInfo->name) . '.php';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
101 View Code Duplication
            if (is_file($pathClass)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
102
                $fsList['file'][$pathClass] = true;
103
            } else {
104
                $fsList['file'][$pathClass] = false;
105
            }
106
107
            //Repository作成
108
            $filename = $this->makeRepository($pluginCode, $MetadataInfo);
109 View Code Duplication
            if (is_file($filename)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
110
                $fsList['file'][$filename] = true;
111
            } else {
112
                $fsList['file'][$filename] = false;
113
            }
114
        }
115
116
        //Migration作成
117
        if (count($metadetas)) {
118
            $migrationContent = $this->makeMigration($pluginCode, $metadetas);
119
            $timeSt = date('YmdHis');
120
            $migrationContent = str_replace('[datetime]', $timeSt, $migrationContent);
121
            $migPath = $codePath . '/Resource/doctrine/migration/Version' . $timeSt . '.php';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
122
123
            file_put_contents($migPath, $migrationContent);
124 View Code Duplication
            if (is_file($migPath)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
125
                $fsList['file'][$migPath] = true;
126
            } else {
127
                $fsList['file'][$migPath] = false;
128
            }
129
        }
130
131
        $dirFileNg = array();
132
        $dirFileOk = array();
133 View Code Duplication
        foreach ($fsList['dir'] as $path => $flag) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
134
            if ($flag) {
135
                $dirFileOk[] = $path;
136
            } else {
137
                $dirFileNg[] = $path;
138
            }
139
        }
140 View Code Duplication
        foreach ($fsList['file'] as $path => $flag) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
141
            if ($flag) {
142
                $dirFileOk[] = $path;
143
            } else {
144
                $dirFileNg[] = $path;
145
            }
146
        }
147
        $this->output->writeln('');
148
        $this->output->writeln('[+]file system');
149 View Code Duplication
        if (!empty($dirFileOk)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
150
            $this->output->writeln('');
151
            $this->output->writeln(' this files and folders were created.');
152
            foreach ($dirFileOk as $path) {
153
                $this->output->writeln('<info> - ' . $path . '</info>');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
154
            }
155
        }
156
    }
157
158 View Code Duplication
    protected function initFildset()
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...
159
    {
160
        $this->paramList = array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
161
            'pluginCode' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
162
                'no' => 1,
163
                 'label' => '[+]Plugin Code: ',
164
                'value' => null,
165
                'name' => '[+]Please enter Plugin Name (only pascal case letters numbers are allowed)',
166
                'validation' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
167
                    'isRequired' => true,
168
                    'inArray' => $this->getPluginList()
169
                )
170
            ),
171
            'entityList' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
172
                'no' => 2,
173
                'label' => '[+]Yaml file name: ',
174
                'value' => array(),
175
                'name' => '[+]Plese enter yaml file name',
176
                'validation' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
177
                    'isRequired' => false,
178
                    'inArray' => 'getEntityList'
179
                )
180
            ),
181
            'supportFlag' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
182
                'no' => 3,
183
                'label' => '[+]Old version support: ',
184
                'value' => null,
185
                'name' => '[+]Do you want to support old versions too? [y/n]',
186
                'show' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
187
                    1 => 'Yes', 0 => 'No'),
188
                'validation' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
189
                    'isRequired' => true,
190
                    'choice' => array('y' => 1, 'n' => 0)
191
                )
192
            )
193
        );
194
    }
195
196
    protected function getEntityList()
197
    {
198
        if (!$this->paramList['pluginCode']['value']) {
199
            return array();
200
        }
201
202
        if ($this->entityList === null) {
203
            $this->entityList = array();
204
            $directory = $this->app['config']['root_dir'] . '/app/Plugin/' . $this->paramList['pluginCode']['value'] . '/Resource/doctrine/';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
205
            if (!is_dir($directory)) {
206
                return $this->entityList;
207
            }
208
            $dirListing = array_diff(scandir($directory), array('..', '.'));
209
            $ext = YamlDriver::DEFAULT_FILE_EXTENSION;
210
            $revCount = strlen($ext) * -1;
211
            foreach ($dirListing as $fileName) {
212
                if (substr($fileName, $revCount) == $ext) {
213
                    $className = str_replace('.', '\\', str_replace($ext, '', $fileName));
214
                    $this->entityList[$fileName] = $className;
215
                }
216
            }
217
        }
218
219
        return $this->entityList;
220
    }
221
222 View Code Duplication
    private function getPluginList()
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...
223
    {
224
        $ret = array();
225
        $pluginDir = $this->app['config']['root_dir'] . '/app/Plugin';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
226
        $iterator = new \DirectoryIterator($pluginDir);
227
        foreach ($iterator as $fileInfo) {
228
            if ($fileInfo->isDot()) {
229
                continue;
230
            }
231
            if ($fileInfo->isDir()) {
232
                $ret[$fileInfo->getFilename()] = $fileInfo->getFilename();
233
            }
234
        }
235
        return $ret;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
236
    }
237
238
    private function toCamelCase($name)
239
    {
240
        return lcfirst(implode('', array_map(function ($name) {
241
                    return ucfirst($name);
242
                }, explode('_', $name))));
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 16.
Loading history...
Coding Style introduced by
Closing brace indented incorrectly; expected 8 spaces, found 16
Loading history...
243
    }
244
245
    private function makeRepository($pluginCode, $metadata)
246
    {
247
        $nameFormated = ucfirst($this->getShortClassName($metadata->table['name']));
248
249
        $line = array();
250
        $line[] = '<?php';
251
        $line[] = '/*';
252
        $line[] = ' * This file is part of ' . $pluginCode;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
253
        $line[] = ' *';
254
        $line[] = ' *';
255
        $line[] = ' * For the full copyright and license information, please view the LICENSE';
256
        $line[] = ' * file that was distributed with this source code.';
257
        $line[] = ' */';
258
        $line[] = '';
259
        $line[] = 'namespace Plugin\\' . $pluginCode . '\Repository;';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
260
        $line[] = '';
261
        $line[] = 'use Doctrine\ORM\EntityRepository;';
262
        $line[] = '';
263
        $line[] = '/**';
264
        $line[] = ' * ' . $nameFormated;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
265
        $line[] = ' *';
266
        $line[] = ' * This class was generated by the Doctrine ORM. Add your own custom';
267
        $line[] = ' * repository methods below.';
268
        $line[] = ' */';
269
        $line[] = 'class ' . $nameFormated . 'Repository extends EntityRepository';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
270
        $line[] = '{';
271
        $line[] = '';
272
        $line[] = '}';
273
        $line[] = '';
274
275
        $cont = join(PHP_EOL, $line);
276
        $filename = $this->app['config']['root_dir'] . '/app/Plugin/' . $pluginCode . '/Repository/' . $nameFormated . 'Repository.php';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
277
        file_put_contents($filename, $cont);
278
        return $filename;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
279
    }
280
281 View Code Duplication
    private function makeMigration($pluginCode, $metadetas)
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...
282
    {
283
        if ($this->paramList['supportFlag']['value']) {
284
            $migrationFileCont = file_get_contents($this->app['config']['root_dir'] . '/src/Eccube/Command/PluginCommand/Resource/MigrationVersionSupport.php');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
285
        } else {
286
            $migrationFileCont = file_get_contents($this->app['config']['root_dir'] . '/src/Eccube/Command/PluginCommand/Resource/MigrationVersion.php');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
287
        }
288
289
        $entityList = $this->createEntityList($pluginCode, $metadetas);
290
291
        $entityListStr = join(',' . PHP_EOL, $entityList);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
292
        $migrationFileCont = str_replace('[entityList]', $entityListStr, $migrationFileCont);
293
        if ($this->paramList['supportFlag']['value']) {
294
            $createParts = $this->makeCreateParts($metadetas);
295
            $tableNameArr = array();
296
            foreach ($createParts as $tableName => $tableArr) {
297
                $tableNameArr[] = '            $this->createTable' . $tableName . '($schema);';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
298
            }
299
            $tableNameStr = join(PHP_EOL, $tableNameArr);
300
            $migrationFileCont = str_replace('[createTable]', $tableNameStr, $migrationFileCont);
301
302
            $createPartsStr = '';
303
            foreach ($createParts as $parts) {
304
                $createPartsStr .= join(PHP_EOL, $parts);
305
            }
306
            $migrationFileCont = str_replace('[createFunction]', $createPartsStr, $migrationFileCont);
307
308
            $dropParts = $this->makeDropParts($metadetas);
309
            $dropPartsStr = join(PHP_EOL, $dropParts);
310
            $migrationFileCont = str_replace('[dropTable]', $dropPartsStr, $migrationFileCont);
311
        }
312
313
        return $migrationFileCont;
314
    }
315
316
    private function createEntityList($pluginCode, $migrations)
317
    {
318
        $ret = array();
319
        foreach ($migrations as $metadata) {
320
            $ret[] = "        '" . 'Plugin\\' . $pluginCode . '\Entity\\' . ucfirst($this->getShortClassName($metadata->table['name'])) . "'";
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
321
        }
322
        return $ret;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
323
    }
324
325
    private function getShortClassName($dbTableName)
326
    {
327
        return $this->toCamelCase(str_replace(self ::PLUGIN_PREFIX, '', $dbTableName));
328
    }
329
330
    private function makeCreateParts($metadetas)
331
    {
332
        $ret = array();
333
        foreach ($metadetas as $metadata) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
334
335
            $nameFormated = $this->getShortClassName($metadata->table['name']);
336
            $tmp = array();
337
            $tmp[] = '';
338
            $tmp[] = '    public function createTable' . ucfirst($nameFormated) . '(Schema $schema)';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
339
            $tmp[] = '    {';
340
            $tmp[] = '        $table = $schema->createTable(\'' . $metadata->table['name'] . '\');';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
341
            $columns = $metadata->fieldMappings;
342
            foreach ($columns as $column) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
343
344
                $typeName = $column['type'];
345
                $tmp[] = '        $table->addColumn(\'' . $column['columnName'] . '\', \'' . $typeName . '\', array(';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
346
                $param = array();
347
                if (isset($column['nullable']) && $column['nullable']) {
348
                    $param['notnull'] = 'true';
349
                } else {
350
                    $param['notnull'] = 'false';
351
                }
352
353 View Code Duplication
                foreach ($param as $parKey => $parVal) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
354
                    $tmp[] = '            \'' . $parKey . '\' => ' . $parVal . ',';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
355
                }
356
                $tmp[] = '        ));';
357
            }
358
359
360
            $tmp[] = '    }';
361
            $tmp[] = '';
362
            $ret[ucfirst($nameFormated)] = $tmp;
363
        }
364
365
        return $ret;
366
    }
367
368
    private function makeDropParts($metadetas)
369
    {
370
        $ret = array();
371
        foreach ($metadetas as $metadata) {
372
            $ret[] = '            $schema->dropTable(\'' . $metadata->table['name'] . '\');';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
373
        }
374
375
        return $ret;
376
    }
377
}
378