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

PluginGenerator::getEvents()   C

Complexity

Conditions 8
Paths 4

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 31
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 47
rs 5.7377
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 Eccube\Common\Constant;
28
use Symfony\Component\Filesystem\Filesystem;
29
use Symfony\Component\Yaml\Yaml;
30
use Eccube\Entity\Plugin;
31
use Eccube\Entity\PluginEventHandler;
32
use Eccube\Command\PluginCommand\AbstractPluginGenerator;
33
34
class PluginGenerator extends AbstractPluginGenerator
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
35
{
36
37
    /**
38
     *
39
     * @var array
40
     */
41
    private $hookPoints = null;
42
43
    /**
44
     *
45
     * @var array
46
     */
47
    private $events = null;
48
49 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...
50
    {
51
        $this->output->writeln('------------------------------------------------------');
52
        $this->output->writeln('---Plugin Generator');
53
        $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...
54
        $this->output->writeln('------------------------------------------------------');
55
        $this->output->writeln('');
56
    }
57
58
    protected function initFildset()
59
    {
60
        $this->paramList = array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
61
            'pluginName' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
62
                'no' => 1,
63
                'label' => '[+]Plugin Name: ',
64
                'value' => null,
65
                'name' => '[+]Please enter Plugin Name',
66
                'validation' => array(
67
                    'isRequired' => true,
68
                )
69
            ),
70
            'pluginCode' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
71
                'no' => 2,
72
                'label' => '[+]Plugin Code: ',
73
                'value' => null,
74
                'name' => '[+]Please enter Plugin Name (only pascal case letters numbers are allowed)',
75
                'validation' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
76
                    'isRequired' => true,
77
                    'patern' => '/^[A-Z][0-9a-zA-Z]*$/'
78
                )
79
            ),
80
            'version' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
81
                'no' => 3,
82
                'label' => '[+]Version: ',
83
                'value' => null,
84
                'name' => '[+]Please enter version (correct format is x.y.z)',
85
                'validation' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
86
                    'isRequired' => true,
87
                    'patern' => '/^\d+.\d+.\d+$/'
88
                )
89
            ),
90
            'author' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
91
                'no' => 4,
92
                'label' => '[+]Author: ',
93
                'value' => null,
94
                'name' => '[+]Please enter author name or company',
95
                'validation' => array(
96
                    'isRequired' => true,
97
                )
98
            ),
99
            'supportFlag' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
100
                'no' => 5,
101
                'label' => '[+]Old version support: ',
102
                'value' => null,
103
                'name' => '[+]Do you want to support old versions too? [y/n]',
104
                'show' => array(1 => 'Yes' ,0 => 'No'),
0 ignored issues
show
introduced by
Add a single space after each comma delimiter
Loading history...
105
                'validation' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
106
                    'isRequired' => true,
107
                    'choice' => array('y' => 1, 'n' => 0)
108
                )
109
            ),
110
            'events' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
111
                'no' => 6,
112
                'label' => '[+]Site events: ',
113
                'value' => array(),
114
                'name' => '[+]Please enter site events(you can find documentation here http://www.ec-cube.net/plugin/)',
115
                'validation' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
116
                    'isRequired' => false,
117
                    'inArray' => 'getEvents'
118
                )
119
            ),
120
            'hookPoints' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
121
                'no' => 7,
122
                'label' => '[+]Hook points: ',
123
                'value' => array(),
124
                'name' => '[+]Please enter hook point, sample:front.cart.up.initialize',
125
                'validation' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
126
                    'isRequired' => false,
127
                    'inArray' => $this->getHookPoints()
128
                )
129
            )
130
        );
131
    }
132
133
    private function getHookPoints()
134
    {
135
        if ($this->hookPoints === null) {
136
            $Ref = new \ReflectionClass('\Eccube\Event\EccubeEvents');
137
            $this->hookPoints = array_flip($Ref->getConstants());
138
        }
139
        return $this->hookPoints;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
140
    }
141
142
    protected function getEvents()
0 ignored issues
show
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
143
    {
144
        if (!isset($this->paramList['supportFlag']['value'])) {
145
            return array();
146
        }
147
        if ($this->events === null) {
148
            $this->events = array();
149
            $routeEvents = array();
150
            if ($this->paramList['supportFlag']['value']) {
151
                $this->events = include $this->app['config']['root_dir'] . '/src/Eccube/Command/PluginCommand/Resource/eventsList.php';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
152
                $routeEvents['eccube.event.controller.__route__.before'] = 'Controller__route__Before';
153
                $routeEvents['eccube.event.controller.__route__.after'] = 'Controller__route__After';
154
                $routeEvents['eccube.event.controller.__route__.finish'] = 'Controller__route__Finish';
155
                $routeEvents['eccube.event.render.__route__.before'] = 'Render__route__Before';
156
            }
157
            $this->events += include $this->app['config']['root_dir'] . '/src/Eccube/Command/PluginCommand/Resource/eventsListNew.php';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
158
159
            $routeEvents['eccube.event.route.__route__.request'] = 'Route__route__Request';
160
            $routeEvents['eccube.event.route.__route__.controller'] = 'Route__route__Controller';
161
            $routeEvents['eccube.event.route.__route__.response'] = 'Route__route__Response';
162
            $routeEvents['eccube.event.route.__route__.exception'] = 'Route__route__Exception';
163
            $routeEvents['eccube.event.route.__route__.terminate'] = 'Route__route__Terminate';
164
            $allRoutes = array();
165
166
            $controllers = $this->app['controllers'];
167
            $collection = $controllers->flush();
168
            foreach ($collection as $eventName => $dummy) {
169
                $allRoutes[] = $eventName;
170
            }
171
172
            $routes = $this->app['routes']->all();
173
174
            foreach ($routes as $eventName => $dummy) {
175
                $allRoutes[] = $eventName;
176
            }
177
178
            foreach ($allRoutes as $eventName) {
179
                $eventOnFunc = join(array_map('ucfirst', explode('_', strtolower($eventName))));
180
181
                foreach ($routeEvents as $keys => $node) {
182
                    $this->events[str_replace('__route__', $eventName, $keys)] = str_replace('__route__', $eventOnFunc, $node);
183
                }
184
            }
185
        }
186
187
        return $this->events;
188
    }
189
190
    protected function start()
191
    {
192
        $pluginCode = $this->paramList['pluginCode']['value'];
193
194
        $Plugin = $this->app['eccube.repository.plugin']->findOneBy(array('code' => $pluginCode));
195
        if ($Plugin) {
196
            $this->exitGenerator('<error>Plugin with this name already exists</error>');
197
            return;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
198
        }
199
        $this->createFilesAndFolders($pluginCode, $this->paramList);
200
        $this->createDbRecords($pluginCode, $this->paramList);
201
        $this->exitGenerator('Plugin was created successfully');
202
    }
203
204
    private function createFilesAndFolders($code, $paramList)
205
    {
206
        $fsList = array(
207
            'dir' => array(),
208
            'file' => array(),
209
        );
210
211
        // config.ymlを作成
212
        $config = array();
213
        $config['name'] = $paramList['pluginName']['value'];
214
        $config['code'] = $code;
215
        $config['version'] = $paramList['version']['value'];
216
        if (!empty($paramList['hookPoints']['value'])) {
217
            $config['event'] = $code . 'Event';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
218
        }
219
        $config['service'] = array($code . 'ServiceProvider');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
220
221
        $codePath = $this->app['config']['root_dir'] . '/app/Plugin/' . $code;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
222
223
        $file = new Filesystem();
224
        $file->mkdir($codePath);
225
        if (is_dir($codePath)) {
226
            $fsList['dir'][$codePath] = true;
227
        } else {
228
            $fsList['dir'][$codePath] = false;
229
        }
230
231
        $srcPath = $codePath . '/config.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
232
        file_put_contents($srcPath, Yaml::dump($config));
233 View Code Duplication
        if (is_file($srcPath)) {
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...
234
            $fsList['file'][$srcPath] = true;
235
        } else {
236
            $fsList['file'][$srcPath] = false;
237
        }
238
239
        $author = $paramList['pluginName']['value'];
240
        $year = date('Y');
241
242
        // PluginManager
243
        $pluginFileBefore = file_get_contents($this->app['config']['root_dir'] . '/src/Eccube/Command/PluginCommand/Resource/PluginManager.php');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
244
        $from = '/\[code\]/';
245
        $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore);
246
        $from = '/\[author\]/';
247
        $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter);
248
        $from = '/\[year\]/';
249
        $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter);
250
251
        $srcPath = $codePath . '/PluginManager.php';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
252
        file_put_contents($srcPath, $pluginFileAfter);
253 View Code Duplication
        if (is_file($srcPath)) {
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...
254
            $fsList['file'][$srcPath] = true;
255
        } else {
256
            $fsList['file'][$srcPath] = false;
257
        }
258
259
        // ServiceProvider
260
        $pluginFileBefore = file_get_contents($this->app['config']['root_dir'] . '/src/Eccube/Command/PluginCommand/Resource/ServiceProvider.php');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
261
        $from = '/\[code\]/';
262
        $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore);
263
        $from = '/\[author\]/';
264
        $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter);
265
        $from = '/\[year\]/';
266
        $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter);
267
268
        $file->mkdir($codePath . '/ServiceProvider');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
269 View Code Duplication
        if (is_dir($codePath . '/ServiceProvider')) {
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...
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
270
            $fsList['dir'][$codePath . '/ServiceProvider'] = true;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
271
        } else {
272
            $fsList['dir'][$codePath . '/ServiceProvider'] = false;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
273
        }
274
275
        $srcPath = $codePath . '/ServiceProvider/' . $code . 'ServiceProvider.php';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
276
        file_put_contents($srcPath, $pluginFileAfter);
277 View Code Duplication
        if (is_file($srcPath)) {
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...
278
            $fsList['file'][$srcPath] = true;
279
        } else {
280
            $fsList['file'][$srcPath] = false;
281
        }
282
283
        // ConfigController
284
        $pluginFileBefore = file_get_contents($this->app['config']['root_dir'] . '/src/Eccube/Command/PluginCommand/Resource/ConfigController.php');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
285
        $from = '/\[code\]/';
286
        $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore);
287
        $from = '/\[author\]/';
288
        $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter);
289
        $from = '/\[year\]/';
290
        $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter);
291
        $from = '/\[code_name\]/';
292
        $pluginFileAfter = preg_replace($from, mb_strtolower($code), $pluginFileAfter);
293
294
        $file->mkdir($codePath . '/Controller');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
295 View Code Duplication
        if (is_dir($codePath . '/Controller')) {
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...
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
296
            $fsList['dir'][$codePath . '/Controller'] = true;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
297
        } else {
298
            $fsList['dir'][$codePath . '/Controller'] = false;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
299
        }
300
301
        $srcPath = $codePath . '/Controller/ConfigController.php';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
302
        file_put_contents($srcPath, $pluginFileAfter);
303 View Code Duplication
        if (is_file($srcPath)) {
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...
304
            $fsList['file'][$srcPath] = true;
305
        } else {
306
            $fsList['file'][$srcPath] = false;
307
        }
308
309
        // Controller
310
        $pluginFileBefore = file_get_contents($this->app['config']['root_dir'] . '/src/Eccube/Command/PluginCommand/Resource/Controller.php');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
311
        $from = '/\[code\]/';
312
        $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore);
313
        $from = '/\[author\]/';
314
        $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter);
315
        $from = '/\[year\]/';
316
        $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter);
317
        $from = '/\[code_name\]/';
318
        $pluginFileAfter = preg_replace($from, mb_strtolower($code), $pluginFileAfter);
319
320
        $srcPath = $codePath . '/Controller/' . $code . 'Controller.php';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
321
        file_put_contents($srcPath, $pluginFileAfter);
322 View Code Duplication
        if (is_file($srcPath)) {
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...
323
            $fsList['file'][$srcPath] = true;
324
        } else {
325
            $fsList['file'][$srcPath] = false;
326
        }
327
328
        // Form
329
        $pluginFileBefore = file_get_contents($this->app['config']['root_dir'] . '/src/Eccube/Command/PluginCommand/Resource/ConfigType.php');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
330
        $from = '/\[code\]/';
331
        $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore);
332
        $from = '/\[author\]/';
333
        $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter);
334
        $from = '/\[year\]/';
335
        $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter);
336
        $from = '/\[code_name\]/';
337
        $pluginFileAfter = preg_replace($from, mb_strtolower($code), $pluginFileAfter);
338
339
        $file->mkdir($codePath . '/Form/Type');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
340 View Code Duplication
        if (is_dir($codePath . '/Form/Type')) {
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...
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
341
            $fsList['dir'][$codePath . '/Form/Type'] = true;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
342
        } else {
343
            $fsList['dir'][$codePath . '/Form/Type'] = false;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
344
        }
345
346
        $srcPath = $codePath . '/Form/Type/' . $code . 'ConfigType.php';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
347
        file_put_contents($codePath . '/Form/Type/' . $code . 'ConfigType.php', $pluginFileAfter);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
348 View Code Duplication
        if (is_file($srcPath)) {
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...
349
            $fsList['file'][$srcPath] = true;
350
        } else {
351
            $fsList['file'][$srcPath] = false;
352
        }
353
354
        // Twig
355
        $pluginFileBefore = file_get_contents($this->app['config']['root_dir'] . '/src/Eccube/Command/PluginCommand/Resource/config.twig');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
356
        $from = '/\[code\]/';
357
        $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore);
358
359
        $file->mkdir($codePath . '/Resource/template/admin');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
360 View Code Duplication
        if (is_dir($codePath . '/Resource/template/admin')) {
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...
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
361
            $fsList['dir'][$codePath . '/Resource/template/admin'] = true;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
362
        } else {
363
            $fsList['dir'][$codePath . '/Resource/template/admin'] = false;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
364
        }
365
366
        $srcPath = $codePath . '/Resource/template/admin/config.twig';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
367
        file_put_contents($srcPath, $pluginFileAfter);
368 View Code Duplication
        if (is_file($srcPath)) {
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...
369
            $fsList['file'][$srcPath] = true;
370
        } else {
371
            $fsList['file'][$srcPath] = false;
372
        }
373
374
        // index.twig
375
        $pluginFileBefore = file_get_contents($this->app['config']['root_dir'] . '/src/Eccube/Command/PluginCommand/Resource/index.twig');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
376
        $from = '/\[code\]/';
377
        $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore);
378
379
        $file->mkdir($codePath . '/Resource/template/admin');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
380 View Code Duplication
        if (is_dir($codePath . '/Resource/template/admin')) {
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...
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
381
            $fsList['dir'][$codePath . '/Resource/template/admin'] = true;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
382
        } else {
383
            $fsList['dir'][$codePath . '/Resource/template/admin'] = false;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
384
        }
385
386
        $srcPath = $codePath . '/Resource/template/index.twig';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
387
        file_put_contents($srcPath, $pluginFileAfter);
388 View Code Duplication
        if (is_file($srcPath)) {
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...
389
            $fsList['file'][$srcPath] = true;
390
        } else {
391
            $fsList['file'][$srcPath] = false;
392
        }
393
394
        $onFunctions = array();
395
        $onEvents = array();
396
397
        //イベント
398
        $events = $paramList['events']['value'];
399
        if (count($events) > 0) {
400
            foreach ($events as $eventKey => $eventConst) {
401
                $onEvents[$eventKey] = array(array('on' . $eventConst . ', NORMAL'));
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
402
                $onFunctions[] = 'on' . $eventConst;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
403
            }
404
        }
405
406
        //フックポイント
407
        $hookPoints = $paramList['hookPoints']['value'];
408
        if (count($hookPoints)) {
409
            foreach ($hookPoints as $hookKey => $hookConst) {
410
                $onName = 'on' . join(array_map('ucfirst', explode('_', strtolower($hookConst))));
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
411
                $onEvents[$hookKey] = array(array($onName . ', NORMAL'));
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
412
                $onFunctions[] = $onName;
413
            }
414
        }
415
416
        if (count($onEvents)) {
417
            $srcPath = $codePath . '/event.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
418
            file_put_contents($srcPath, str_replace('\'', '', Yaml::dump($onEvents)));
419 View Code Duplication
            if (is_file($srcPath)) {
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...
420
                $fsList['file'][$srcPath] = true;
421
            } else {
422
                $fsList['file'][$srcPath] = false;
423
            }
424
425
            $pluginFileBefore = file_get_contents($this->app['config']['root_dir'] . '/src/Eccube/Command/PluginCommand/Resource/EventHookpoint2.php');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
426
427
            // Event
428
            $from = '/\[code\]/';
429
            $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore);
430
            $from = '/\[author\]/';
431
            $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter);
432
            $from = '/\[year\]/';
433
            $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter);
434
435
            $functions = '';
436
            foreach ($onFunctions as $functionName) {
437
                $functions .= "    public function " . $functionName . "(EventArgs \$event)\n    {\n    }\n\n";
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
438
            }
439
            $from = '/\[hookpoint_function\]/';
440
            $pluginFileAfter = preg_replace($from, $functions, $pluginFileAfter);
441
            $srcPath = $codePath . '/' . $code . 'Event.php';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
442
            file_put_contents($srcPath, $pluginFileAfter);
443 View Code Duplication
            if (is_file($srcPath)) {
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...
444
                $fsList['file'][$srcPath] = true;
445
            } else {
446
                $fsList['file'][$srcPath] = false;
447
            }
448
        }
449
450
        // config.ymlを再作成
451
        $config = array();
452
        $config['name'] = $paramList['pluginName']['value'];
453
        $config['code'] = $code;
454
        $config['version'] = $paramList['version']['value'];
455
        $config['event'] = $code . 'Event';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
456
        $config['service'] = array($code . 'ServiceProvider');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
457
        $srcPath = $codePath . '/config.yml';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
458
        file_put_contents($srcPath, Yaml::dump($config));
459 View Code Duplication
        if (is_file($srcPath)) {
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...
460
            $fsList['file'][$srcPath] = true;
461
        } else {
462
            $fsList['file'][$srcPath] = false;
463
        }
464
465
        // LICENSE
466
        $srcPath = $codePath . '/LICENSE';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
467
        $file->copy($this->app['config']['root_dir'] . '/src/Eccube/Command/PluginCommand/Resource/LICENSE', $srcPath);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
468 View Code Duplication
        if (is_file($srcPath)) {
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...
469
            $fsList['file'][$srcPath] = true;
470
        } else {
471
            $fsList['file'][$srcPath] = false;
472
        }
473
474
        $dirFileNg = array();
475
        $dirFileOk = array();
476 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...
477
            if ($flag) {
478
                $dirFileOk[] = $path;
479
            } else {
480
                $dirFileNg[] = $path;
481
            }
482
        }
483 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...
484
            if ($flag) {
485
                $dirFileOk[] = $path;
486
            } else {
487
                $dirFileNg[] = $path;
488
            }
489
        }
490
        $this->output->writeln('');
491
        $this->output->writeln('[+]File system');
492 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...
493
            $this->output->writeln('');
494
            $this->output->writeln(' this files and folders were created.');
495
            foreach ($dirFileOk as $path) {
496
                $this->output->writeln('<info> - ' . $path . '</info>');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
497
            }
498
        }
499
500 View Code Duplication
        if (!empty($dirFileNg)) {
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...
501
            $this->output->writeln('');
502
            $this->output->writeln(' this files and folders was not created.');
503
            foreach ($dirFileOk as $path) {
504
                $this->output->writeln('<error> - ' . $path . '</error>');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
505
            }
506
        }
507
    }
508
509
    private function createDbRecords($code, $paramList)
510
    {
511
        // DB登録
512
        $Plugin = new Plugin();
513
        $Plugin->setName($paramList['pluginName']['value']);
514
        $Plugin->setCode($code);
515
        $Plugin->setClassName('');
516
        $Plugin->setVersion($paramList['version']['value']);
517
        $Plugin->setEnable(Constant::DISABLED);
518
        $Plugin->setSource(0);
519
        $Plugin->setDelFlg(Constant::DISABLED);
520
521
        $this->app['orm.em']->persist($Plugin);
522
        $this->app['orm.em']->flush($Plugin);
523
524
        $this->output->writeln('');
525
        $this->output->writeln('[+]Database');
526
        if ($Plugin->getId()) {
527
            $this->output->writeln('<info> Plugin information was added to table [DB.Plugin] (id=' . $Plugin->getId() . ')</info>');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
528
        } else {
529
            $this->output->writeln('<error> there was a problem inserting plugin information to table [DB.Plugin] (id=' . $Plugin->getId() . ')</error>');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
530
        }
531
532
        $hookPoints = $paramList['hookPoints']['value'];
533
        if (empty($hookPoints)) {
534
            return;
535
        }
536
537
        $eventCount = 0;
538
        foreach ($hookPoints as $hookKey => $hookConst) {
539
            $PluginEventHandler = new PluginEventHandler();
540
            $functionName = 'on' . join(array_map('ucfirst', explode('_', strtolower($hookConst))));
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
541
            $PluginEventHandler->setPlugin($Plugin)
542
                ->setEvent($hookKey)
543
                ->setPriority($this->app['eccube.repository.plugin_event_handler']->calcNewPriority($hookKey, $functionName))
544
                ->setHandler($functionName)
545
                ->setHandlerType('NORMAL')
546
                ->setDelFlg(Constant::DISABLED);
547
            $this->app['orm.em']->persist($PluginEventHandler);
548
            $eventCount++;
549
        }
550
        $this->app['orm.em']->flush();
551
        if ($eventCount) {
552
            $this->output->writeln('');
553
            $this->output->writeln('<info> Plugin information was added to table [DB.PluginEventHandler] (inserts number=' . $eventCount . ') </info>');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
554
        }
555
    }
556
}
557