Failed Conditions
Push — master ( d32578...ed56ec )
by Ryo
78:15
created

EntityFromDbGenerator::start()   C

Complexity

Conditions 8
Paths 26

Size

Total Lines 92
Code Lines 53

Duplication

Lines 11
Ratio 11.96 %

Importance

Changes 0
Metric Value
nc 26
dl 11
loc 92
c 0
b 0
f 0
cc 8
eloc 53
nop 0
rs 5.538

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\GeneratorCommand;
26
27
use Doctrine\Common\Util\Inflector;
28
use Doctrine\ORM\Mapping\ClassMetadataInfo;
29
use Doctrine\ORM\Mapping\Driver\DatabaseDriver;
30
use Doctrine\ORM\Tools\Console\MetadataFilter;
31
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
32
use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
33
use Symfony\Component\Finder\Finder;
34
35
class EntityFromDbGenerator extends AbstractPluginGenerator
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
36
{
37
38
    /**
39
     * テーブルリスト
40
     *
41
     * @var array
42
     */
43
    private $tableList = 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 table schema first.');
50
        $this->output->writeln('---[*]You can exit from Console Application, by typing '.self::STOP_PROCESS.' instead of typing another word.');
51
        $this->output->writeln('------------------------------------------------------');
52
        $this->output->writeln('');
53
    }
54
55 View Code Duplication
    protected function initFieldSet()
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...
56
    {
57
        $this->paramList = array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
58
            'pluginCode' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
59
                'no' => 1,
60
                'label' => '[+]Plugin Code: ',
61
                'value' => null,
62
                'name' => '[+]Please enter Plugin Code (First letter is uppercase alphabet only. alphabet and numbers are allowed.)',
63
                'validation' => array(
64
                    'isRequired' => true,
65
                    'isNotCode' => $this->getPluginCodes(),
66
                )
67
            ),
68
            'tableList' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
69
                'no' => 2,
70
                'label' => '[+]Table name: ',
71
                'value' => array(),
72
                'name' => '[+]Please enter table name',
73
                'validation' => array(
74
                    'isRequired' => false,
75
                    'inArray' => $this->getTableList(),
76
                )
77
            ),
78
            'supportFlag' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
79
                'no' => 3,
80
                'label' => '[+]Old version support: ',
81
                'value' => null,
82
                'name' => '[+]Do you want to support old versions too? [y/n]',
83
                'show' => array(1 => 'Yes', 0 => 'No'),
84
                'validation' => array(
85
                    'isRequired' => true,
86
                    'choice' => array('y' => 1, 'n' => 0),
87
                )
88
            )
89
        );
90
    }
91
92
    /**
93
     * プラグイン用テーブル一覧(plg_xxxx)の取得
94
     *
95
     * @return array
96
     */
97
    protected function getTableList()
98
    {
99
        if ($this->tableList === null) {
100
            $this->tableList = array();
101
            $SchemaManager = $this->app['orm.em']->getConnection()->getSchemaManager();
102
            foreach ($SchemaManager->listTables() as $Table) {
103
                $tableName = $Table->getName();
104
                if (strpos($tableName, self::PLUGIN_PREFIX) !== 0) {
105
                    continue;
106
                }
107
                $this->tableList[$tableName] = $tableName;
108
            }
109
        }
110
111
        return $this->tableList;
112
    }
113
114
    protected function start()
115
    {
116
        $pluginCode = $this->paramList['pluginCode']['value'];
117
118
        $codes = $this->getPluginCodes();
119
        if (!in_array($pluginCode, $codes)) {
120
            $this->exitGenerator('<error>This plugin code does not exist.</error>');
121
122
            return;
123
        }
124
125
        $fsList = array(
126
            'dir' => array(),
127
            'file' => array(),
128
        );
129
130
        $tableList = $this->paramList['tableList']['value'];
131
        $codePath = $this->app['config']['root_dir'].'/app/Plugin/'.$pluginCode;
132
133
        $dirList = array('Entity', 'Repository', 'Resource', 'Resource/doctrine', 'Resource/doctrine/migration');
134 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...
135
            $dirPath = $codePath.'/'.$dirName;
136
            if (!is_dir($dirPath)) {
137
                mkdir($dirPath);
138
            }
139
            if (is_dir($dirPath)) {
140
                $fsList['dir'][$dirPath] = true;
141
            } else {
142
                $fsList['dir'][$dirPath] = false;
143
            }
144
        }
145
146
        $doctrinePath = $codePath.'/Resource/doctrine';
147
148
        if (count($tableList)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
149
150
            // ymlファイルの作成
151
            $em = $this->app['orm.em'];
152
153
            $databaseDriver = new DatabaseDriver(
154
                $em->getConnection()->getSchemaManager()
155
            );
156
157
            $em->getConfiguration()->setMetadataDriverImpl(
158
                $databaseDriver
159
            );
160
161
            $databaseDriver->setNamespace('Plugin\\'.$pluginCode.'\\Entity\\');
162
163
            $cmf = new DisconnectedClassMetadataFactory();
164
            $cmf->setEntityManager($em);
165
            $metadatas = $cmf->getAllMetadata();
166
167
            $filters = array_map(function ($value) {
168
                return ucfirst(Inflector::camelize(str_replace('plg_', '', $value)));
169
            }, $tableList);
170
            $metadatas = MetadataFilter::filter($metadatas, $filters);
171
172
            $cme = new ClassMetadataExporter();
173
            $exporter = $cme->getExporter('yml', $doctrinePath);
174
175
            /** @var ClassMetadataInfo $class */
176
            foreach ($metadatas as $class) {
177
                $class->name = str_replace('Plg', '', $class->name);
178
                $class->rootEntityName = str_replace('Plg', '', $class->rootEntityName);
179
                $name = explode('\\', $class->name);
180
                $class->customRepositoryClassName = 'Plugin\\'.$pluginCode.'\\Repository\\'.$name[count($name) - 1].'Repository';
181
                $class->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
182
            }
183
184
            $exporter->setMetadata($metadatas);
185
            $exporter->export();
186
187
            $finder = new Finder();
188
            $finder->files()->depth('== 0');
189
            $files = $finder->in($doctrinePath);
190
191
            foreach ($files as $item) {
192
                $fsList['file'][$item->getRealPath()] = true;
193
            }
194
195
            // Entity、Repositoryファイルの作成
196
            $this->generateEntities($metadatas, $fsList);
197
198
            // migrationファイルの作成
199
            $this->generateMigration($metadatas, $fsList, $pluginCode, $codePath);
200
201
            // 完了メッセージ
202
            $this->completeMessage($fsList);
203
        }
204
205
    }
206
207
}
208