Completed
Push — master ( f4c9fc...c12d12 )
by Kristof
19:44 queued 06:26
created

DefaultSiteGenerator   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 444
Duplicated Lines 8.11 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 45
lcom 1
cbo 6
dl 36
loc 444
rs 8.8
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 30 1
A generateAdminLists() 0 9 2
A generateEntities() 18 37 4
A generateFormTypes() 18 36 4
A generateMenuAdaptors() 0 28 4
A generateFixtures() 0 15 2
A generatePagepartConfigs() 0 21 4
A generatePagetemplateConfigs() 0 19 4
A generateConfig() 0 16 3
A generateRouting() 0 14 2
B generateTemplates() 0 69 8
A generateTwigExtensions() 0 16 3
A isMultiLangEnvironment() 0 13 2
A generateControllers() 0 14 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like DefaultSiteGenerator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DefaultSiteGenerator, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Kunstmaan\GeneratorBundle\Generator;
4
5
use Kunstmaan\GeneratorBundle\Helper\GeneratorUtils;
6
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
7
use Symfony\Component\Yaml\Yaml;
8
9
/**
10
 * Generates a default website using several Kunstmaan bundles using default templates and assets
11
 */
12
class DefaultSiteGenerator extends KunstmaanGenerator
13
{
14
    /**
15
     * @var BundleInterface
16
     */
17
    private $bundle;
18
19
    /**
20
     * @var string
21
     */
22
    private $prefix;
23
24
    /**
25
     * @var string
26
     */
27
    private $rootDir;
28
29
    /**
30
     * @var bool
31
     */
32
    private $demosite;
33
34
    /**
35
     * Generate the website.
36
     *
37
     * @param BundleInterface $bundle
38
     * @param string          $prefix
39
     * @param string          $rootDir
40
     * @param bool            $demosite
41
     */
42
    public function generate(BundleInterface $bundle, $prefix, $rootDir, $demosite = false)
43
    {
44
        $this->bundle = $bundle;
45
        $this->prefix = GeneratorUtils::cleanPrefix($prefix);
46
        $this->rootDir = $rootDir;
47
        $this->demosite = $demosite;
48
49
        $parameters = array(
50
            'namespace' => $this->bundle->getNamespace(),
51
            'bundle' => $this->bundle,
52
            'bundle_name' => $this->bundle->getName(),
53
            'prefix' => $this->prefix,
54
            'demosite' => $this->demosite,
55
            'multilanguage' => $this->isMultiLangEnvironment(),
56
            'isV4' => $this->isSymfony4(),
57
        );
58
59
        $this->generateControllers($parameters);
60
        $this->generateAdminLists($parameters);
61
        $this->generateEntities($parameters);
62
        $this->generateFormTypes($parameters);
63
        $this->generateTwigExtensions($parameters);
64
        $this->generateMenuAdaptors($parameters);
65
        $this->generateFixtures($parameters);
66
        $this->generatePagepartConfigs($parameters);
67
        $this->generatePagetemplateConfigs($parameters);
68
        $this->generateConfig();
69
        $this->generateRouting($parameters);
70
        $this->generateTemplates($parameters);
71
    }
72
73
    /**
74
     * Generate controller classes.
75
     *
76
     * @param array $parameters
77
     */
78
    private function generateControllers(array $parameters)
79
    {
80
        $relPath = '/Controller/';
81
        $sourceDir = $this->skeletonDir.$relPath;
82
        $targetDir = $this->bundle->getPath().$relPath;
83
84
        $this->renderSingleFile($sourceDir, $targetDir, 'DefaultController.php', $parameters, true);
85
86
        if ($this->demosite) {
87
            $this->renderSingleFile($sourceDir, $targetDir, 'BikeAdminListController.php', $parameters, true);
88
        }
89
90
        $this->assistant->writeLine('Generating controllers : <info>OK</info>');
91
    }
92
93
    /**
94
     * Generate admin list classes.
95
     *
96
     * @param array $parameters
97
     */
98
    private function generateAdminLists(array $parameters)
99
    {
100
        if ($this->demosite) {
101
            $relPath = '/AdminList/';
102
            $this->renderFiles($this->skeletonDir.$relPath, $this->bundle->getPath().$relPath, $parameters, true);
103
104
            $this->assistant->writeLine('Generating admin lists : <info>OK</info>');
105
        }
106
    }
107
108
    /**
109
     * Generate the entity classes.
110
     *
111
     * @param array $parameters The template parameters
112
     */
113
    public function generateEntities(array $parameters)
114
    {
115
        $relPath = '/Entity/Pages/';
116
        $sourceDir = $this->skeletonDir.$relPath;
117
        $targetDir = $this->bundle->getPath().$relPath;
118
119
        $this->renderSingleFile($sourceDir, $targetDir, 'HomePage.php', $parameters);
120
        $this->renderSingleFile($sourceDir, $targetDir, 'ContentPage.php', $parameters);
121
        $this->renderSingleFile($sourceDir, $targetDir, 'BehatTestPage.php', $parameters);
122
123
        if ($this->demosite) {
124
            $this->renderSingleFile($sourceDir, $targetDir, 'FormPage.php', $parameters);
125
            $this->renderSingleFile($sourceDir, $targetDir, 'SearchPage.php', $parameters);
126
        }
127
128 View Code Duplication
        if ($this->demosite) {
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...
129
            $relPath = '/Entity/PageParts/';
130
            $sourceDir = $this->skeletonDir.$relPath;
131
            $targetDir = $this->bundle->getPath().$relPath;
132
133
            $this->renderSingleFile($sourceDir, $targetDir, 'PageBannerPagePart.php', $parameters);
134
            $this->renderSingleFile($sourceDir, $targetDir, 'ServicePagePart.php', $parameters);
135
            $this->renderSingleFile($sourceDir, $targetDir, 'UspPagePart.php', $parameters);
136
            $this->renderSingleFile($sourceDir, $targetDir, 'BikesListPagePart.php', $parameters);
137
        }
138
139 View Code Duplication
        if ($this->demosite) {
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...
140
            $relPath = '/Entity/';
141
            $sourceDir = $this->skeletonDir.$relPath;
142
            $targetDir = $this->bundle->getPath().$relPath;
143
144
            $this->renderSingleFile($sourceDir, $targetDir, 'Bike.php', $parameters);
145
            $this->renderSingleFile($sourceDir, $targetDir, 'UspItem.php', $parameters);
146
        }
147
148
        $this->assistant->writeLine('Generating entities : <info>OK</info>');
149
    }
150
151
    /**
152
     * Generate the form type classes.
153
     *
154
     * @param array $parameters The template parameters
155
     */
156
    public function generateFormTypes(array $parameters)
157
    {
158
        $relPath = '/Form/Pages/';
159
        $sourceDir = $this->skeletonDir.$relPath;
160
        $targetDir = $this->bundle->getPath().$relPath;
161
162
        $this->renderSingleFile($sourceDir, $targetDir, 'HomePageAdminType.php', $parameters);
163
        $this->renderSingleFile($sourceDir, $targetDir, 'ContentPageAdminType.php', $parameters);
164
        $this->renderSingleFile($sourceDir, $targetDir, 'BehatTestPageAdminType.php', $parameters);
165
166
        if ($this->demosite) {
167
            $this->renderSingleFile($sourceDir, $targetDir, 'FormPageAdminType.php', $parameters);
168
        }
169
170 View Code Duplication
        if ($this->demosite) {
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...
171
            $relPath = '/Form/PageParts/';
172
            $sourceDir = $this->skeletonDir.$relPath;
173
            $targetDir = $this->bundle->getPath().$relPath;
174
175
            $this->renderSingleFile($sourceDir, $targetDir, 'PageBannerPagePartAdminType.php', $parameters);
176
            $this->renderSingleFile($sourceDir, $targetDir, 'ServicePagePartAdminType.php', $parameters);
177
            $this->renderSingleFile($sourceDir, $targetDir, 'UspPagePartAdminType.php', $parameters);
178
            $this->renderSingleFile($sourceDir, $targetDir, 'BikesListPagePartAdminType.php', $parameters);
179
        }
180
181 View Code Duplication
        if ($this->demosite) {
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...
182
            $relPath = '/Form/';
183
            $sourceDir = $this->skeletonDir.$relPath;
184
            $targetDir = $this->bundle->getPath().$relPath;
185
186
            $this->renderSingleFile($sourceDir, $targetDir, 'BikeAdminType.php', $parameters);
187
            $this->renderSingleFile($sourceDir, $targetDir, 'UspItemAdminType.php', $parameters);
188
        }
189
190
        $this->assistant->writeLine('Generating form types : <info>OK</info>');
191
    }
192
193
    /**
194
     * Generate the menu adaptors classes.
195
     *
196
     * @param array $parameters The template parameters
197
     */
198
    public function generateMenuAdaptors(array $parameters)
199
    {
200
        if ($this->demosite) {
201
            $relPath = '/Helper/Menu/';
202
            $sourceDir = $this->skeletonDir.$relPath;
203
            $targetDir = $this->bundle->getPath().$relPath;
204
205
            $this->renderSingleFile($sourceDir, $targetDir, 'AdminMenuAdaptor.php', $parameters);
206
207
            if ($this->isSymfony4()) {
208
                return;
209
            }
210
211
            $file = $this->bundle->getPath().'/Resources/config/services.yml';
212
            if (!is_file($file)) {
213
                $ymlData = 'services:';
214
            } else {
215
                $ymlData = '';
216
            }
217
            $ymlData .= "\n\n    ".strtolower($this->bundle->getName()).'.admin_menu_adaptor:';
218
            $ymlData .= "\n        class: ".$this->bundle->getNamespace()."\Helper\Menu\AdminMenuAdaptor";
219
            $ymlData .= "\n        tags:";
220
            $ymlData .= "\n            -  { name: 'kunstmaan_admin.menu.adaptor' }\n";
221
            file_put_contents($file, $ymlData, FILE_APPEND);
222
223
            $this->assistant->writeLine('Generating menu adaptors : <info>OK</info>');
224
        }
225
    }
226
227
    /**
228
     * Generate the data fixtures classes.
229
     *
230
     * @param array $parameters The template parameters
231
     */
232
    public function generateFixtures(array $parameters)
233
    {
234
        $relPath = '/DataFixtures/ORM/DefaultSiteGenerator/';
235
        $sourceDir = $this->skeletonDir.$relPath;
236
        $targetDir = $this->bundle->getPath().$relPath;
237
238
        $this->renderSingleFile($sourceDir, $targetDir, 'DefaultSiteFixtures.php', $parameters);
239
240
        if ($this->demosite) {
241
            $this->renderSingleFile($sourceDir, $targetDir, 'SliderFixtures.php', $parameters);
242
            $this->renderSingleFile($sourceDir, $targetDir, 'SitemapFixtures.php', $parameters);
243
        }
244
245
        $this->assistant->writeLine('Generating fixtures : <info>OK</info>');
246
    }
247
248
    /**
249
     * Generate the pagepart section configuration.
250
     *
251
     * @param array $parameters The template parameters
252
     */
253
    public function generatePagepartConfigs(array $parameters)
254
    {
255
        $basePath = $this->isSymfony4() ? $this->container->getParameter('kernel.project_dir') : $this->bundle->getPath();
256
        $relPath = $this->isSymfony4() ? '/config/kunstmaancms/pageparts' : '/Resources/config/pageparts/';
257
        $sourceDir = $this->skeletonDir.'/Resources/config/pageparts/';
258
        $targetDir = $basePath.$relPath;
259
260
        $this->renderSingleFile($sourceDir, $targetDir, 'main.yml', $parameters);
261
262
        if ($this->demosite) {
263
            $this->renderSingleFile($sourceDir, $targetDir, 'header.yml', $parameters);
264
            $this->renderSingleFile($sourceDir, $targetDir, 'section1.yml', $parameters);
265
            $this->renderSingleFile($sourceDir, $targetDir, 'section2.yml', $parameters);
266
            $this->renderSingleFile($sourceDir, $targetDir, 'section3.yml', $parameters);
267
            $this->renderSingleFile($sourceDir, $targetDir, 'section4.yml', $parameters);
268
            $this->renderSingleFile($sourceDir, $targetDir, 'section5.yml', $parameters);
269
            $this->renderSingleFile($sourceDir, $targetDir, 'form.yml', $parameters);
270
        }
271
272
        $this->assistant->writeLine('Generating pagepart configuration : <info>OK</info>');
273
    }
274
275
    /**
276
     * Generate the page template configuration.
277
     *
278
     * @param array $parameters The template parameters
279
     */
280
    public function generatePagetemplateConfigs(array $parameters)
281
    {
282
        $basePath = $this->isSymfony4() ? $this->container->getParameter('kernel.project_dir') : $this->bundle->getPath();
283
        $relPath = $this->isSymfony4() ? '/config/kunstmaancms/pagetemplates/' : '/Resources/config/pagetemplates/';
284
        $sourceDir = $this->skeletonDir.'/Resources/config/pagetemplates/';
285
        $targetDir = $basePath.$relPath;
286
287
        $this->renderSingleFile($sourceDir, $targetDir, 'homepage.yml', $parameters);
288
        $this->renderSingleFile($sourceDir, $targetDir, 'contentpage.yml', $parameters);
289
        $this->renderSingleFile($sourceDir, $targetDir, 'behat-test-page.yml', $parameters);
290
291
        if ($this->demosite) {
292
            $this->renderSingleFile($sourceDir, $targetDir, 'contentpage-with-submenu.yml', $parameters);
293
            $this->renderSingleFile($sourceDir, $targetDir, 'formpage.yml', $parameters);
294
            $this->renderSingleFile($sourceDir, $targetDir, 'searchpage.yml', $parameters);
295
        }
296
297
        $this->assistant->writeLine('Generating pagetemplate configuration : <info>OK</info>');
298
    }
299
300
    /**
301
     * Append to the application config file.
302
     */
303
    public function generateConfig()
304
    {
305
        if ($this->isSymfony4()) {
306
            return;
307
        }
308
309
        $configFile = $this->rootDir.'/app/config/config.yml';
310
        $config = file_get_contents($configFile);
311
312
        $data = Yaml::parse($config);
313
        if (!array_key_exists('white_october_pagerfanta', $data)) {
314
            $ymlData = "\n\nwhite_october_pagerfanta:";
315
            $ymlData .= "\n    default_view: twitter_bootstrap\n";
316
            file_put_contents($configFile, $ymlData, FILE_APPEND);
317
        }
318
    }
319
320
    /**
321
     * Generate bundle routing configuration.
322
     *
323
     * @param array $parameters The template parameters
324
     */
325
    public function generateRouting(array $parameters)
326
    {
327
        if ($this->isSymfony4()) {
328
            return;
329
        }
330
331
        $relPath = '/Resources/config/';
332
        $sourceDir = $this->skeletonDir.$relPath;
333
        $targetDir = $this->bundle->getPath().$relPath;
334
335
        $this->renderSingleFile($sourceDir, $targetDir, 'routing.yml', $parameters, true);
336
337
        $this->assistant->writeLine('Generating routing : <info>OK</info>');
338
    }
339
340
    /**
341
     * Generate the twig templates.
342
     *
343
     * @param array $parameters The template parameters
344
     */
345
    public function generateTemplates(array $parameters)
346
    {
347
        $relPath = '/Resources/views/Layout/';
348
        $sourceDir = $this->skeletonDir.$relPath;
349
        $targetDir = $this->getTemplateDir($this->bundle) . '/Layout/';
350
351
        if ($this->demosite) {
352
            $this->renderSingleFile($sourceDir, $targetDir, 'submenu.html.twig', $parameters);
353
        }
354
355
        // Pages
356
357
        $relPath = '/Resources/views/Pages/HomePage/';
358
        $sourceDir = $this->skeletonDir.$relPath;
359
        $targetDir = $this->getTemplateDir($this->bundle) . '/Pages/HomePage/';
360
361
        $this->renderSingleFile($sourceDir, $targetDir, 'pagetemplate.html.twig', $parameters);
362
        $this->renderSingleFile($sourceDir, $targetDir, 'view.html.twig', $parameters);
363
364
        $relPath = '/Resources/views/Pages/ContentPage/';
365
        $this->renderFiles($this->skeletonDir.$relPath, $this->getTemplateDir($this->bundle) . '/Pages/ContentPage/', $parameters, true);
366
367
        if ($this->demosite) {
368
            $relPath = '/Resources/views/Pages/FormPage/';
369
            $this->renderFiles($this->skeletonDir.$relPath, $this->getTemplateDir($this->bundle) . '/Pages/FormPage/', $parameters, true);
370
371
            $relPath = '/Resources/views/Pages/SearchPage/';
372
            $this->renderFiles($this->skeletonDir.$relPath, $this->getTemplateDir($this->bundle) . '/Pages/SearchPage/', $parameters, true);
373
        }
374
375
        // Pageparts
376
377
        if ($this->demosite) {
378
            $relPath = '/Resources/views/PageParts/PageBannerPagePart/';
379
            $this->renderFiles($this->skeletonDir.$relPath, $this->getTemplateDir($this->bundle) . '/PageParts/PageBannerPagePart/', $parameters, true);
380
381
            $relPath = '/Resources/views/PageParts/ServicePagePart/';
382
            $this->renderFiles($this->skeletonDir.$relPath, $this->getTemplateDir($this->bundle) . '/PageParts/ServicePagePart/', $parameters, true);
383
384
            $relPath = '/Resources/views/PageParts/UspPagePart/';
385
            $this->renderFiles($this->skeletonDir.$relPath, $this->getTemplateDir($this->bundle) . '/PageParts/UspPagePart/', $parameters, true);
386
387
            $relPath = '/Resources/views/PageParts/BikesListPagePart/';
388
            $this->renderFiles($this->skeletonDir.$relPath, $this->getTemplateDir($this->bundle) . '/PageParts/BikesListPagePart/', $parameters, true);
389
        }
390
391
        // Error templates
392
        $relPath = '/Resources/views/Error/';
393
        $this->renderFiles($this->skeletonDir.$relPath, $this->getTemplateDir($this->bundle) . '/Error/', $parameters, true);
394
395
        $sourcePath = '/app/TwigBundle/';
396
        $targetPath = $this->isSymfony4() ? $this->rootDir . '/templates/bundles/TwigBundle' : $this->rootDir . '/app/Resources/TwigBundle/';
397
        $this->renderFiles($this->skeletonDir . $sourcePath, $targetPath, $parameters, true);
398
399
        // Bundle overwrites
400
401
        if ($this->demosite) {
402
            $sourcePath = '/app/KunstmaanSitemapBundle/';
403
            $targetPath = $this->isSymfony4() ? $this->rootDir . '/templates/bundles/KunstmaanSitemapBundle' : $this->rootDir . '/app/Resources/KunstmaanSitemapBundle/';
404
405
            $this->renderFiles($this->skeletonDir . $sourcePath, $targetPath, $parameters, true);
406
407
            $sourcePath = '/app/KunstmaanFormBundle/';
408
            $targetPath = $this->isSymfony4() ? $this->rootDir . '/templates/bundles/KunstmaanFormBundle' : $this->rootDir . '/app/Resources/KunstmaanFormBundle/';
409
            $this->renderFiles($this->skeletonDir . $sourcePath, $targetPath, $parameters, true);
410
        }
411
412
        $this->assistant->writeLine('Generating template files : <info>OK</info>');
413
    }
414
415
    /**
416
     * Generate the twig extensions.
417
     *
418
     * @param array $parameters The template parameters
419
     */
420
    public function generateTwigExtensions($parameters)
421
    {
422
        $relPath = '/Twig/';
423
        if ($this->demosite) {
424
            $this->renderSingleFile($this->skeletonDir.$relPath, $this->bundle->getPath().$relPath, 'BikesTwigExtension.php', $parameters, true);
425
        }
426
427
        if ($this->isSymfony4()) {
428
            return;
429
        }
430
431
        $relPath = '/Resources/config/';
432
        $sourceDir = $this->skeletonDir.$relPath;
433
        $targetDir = $this->bundle->getPath().$relPath;
434
        $this->renderSingleFile($sourceDir, $targetDir, 'services.yml', $parameters, true);
435
    }
436
437
    /**
438
     * Returns true if we detect the site uses the locale.
439
     *
440
     * @return bool
441
     */
442
    private function isMultiLangEnvironment()
443
    {
444
        // use the multilanguage parameter, if it exists
445
        if ($this->container->hasParameter('multilanguage')) {
446
            return $this->container->getParameter('multilanguage');
447
        }
448
449
        // This is a pretty silly implementation.
450
        // It just checks if it can find _locale in the routing.yml
451
        $routingFile = file_get_contents($this->rootDir.'/app/config/routing.yml');
452
453
        return preg_match('/_locale:/i', $routingFile);
454
    }
455
}
456