Completed
Push — master ( b453a3...b1e9d6 )
by Ruud
40:05 queued 27:14
created

DefaultSiteGenerator   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 444
Duplicated Lines 11.26 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 55.95%

Importance

Changes 0
Metric Value
wmc 45
lcom 1
cbo 6
dl 50
loc 444
ccs 127
cts 227
cp 0.5595
rs 8.8
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 30 1
A generateControllers() 14 14 2
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

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 1
    public function generate(BundleInterface $bundle, $prefix, $rootDir, $demosite = false)
43
    {
44 1
        $this->bundle = $bundle;
45 1
        $this->prefix = GeneratorUtils::cleanPrefix($prefix);
46 1
        $this->rootDir = $rootDir;
47 1
        $this->demosite = $demosite;
48
49
        $parameters = array(
50 1
            'namespace' => $this->bundle->getNamespace(),
51 1
            'bundle' => $this->bundle,
52 1
            'bundle_name' => $this->bundle->getName(),
53 1
            'prefix' => $this->prefix,
54 1
            'demosite' => $this->demosite,
55 1
            'multilanguage' => $this->isMultiLangEnvironment(),
56 1
            'isV4' => $this->isSymfony4(),
57
        );
58
59 1
        $this->generateControllers($parameters);
60 1
        $this->generateAdminLists($parameters);
61 1
        $this->generateEntities($parameters);
62 1
        $this->generateFormTypes($parameters);
63 1
        $this->generateTwigExtensions($parameters);
64 1
        $this->generateMenuAdaptors($parameters);
65 1
        $this->generateFixtures($parameters);
66 1
        $this->generatePagepartConfigs($parameters);
67 1
        $this->generatePagetemplateConfigs($parameters);
68 1
        $this->generateConfig();
69 1
        $this->generateRouting($parameters);
70 1
        $this->generateTemplates($parameters);
71 1
    }
72
73
    /**
74
     * Generate controller classes.
75
     *
76
     * @param array $parameters
77
     */
78 1 View Code Duplication
    private function generateControllers(array $parameters)
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...
79
    {
80 1
        $relPath = '/Controller/';
81 1
        $sourceDir = $this->skeletonDir.$relPath;
82 1
        $targetDir = $this->bundle->getPath().$relPath;
83
84 1
        $this->renderSingleFile($sourceDir, $targetDir, 'DefaultController.php', $parameters, true);
85
86 1
        if ($this->demosite) {
87
            $this->renderSingleFile($sourceDir, $targetDir, 'BikeAdminListController.php', $parameters, true);
88
        }
89
90 1
        $this->assistant->writeLine('Generating controllers : <info>OK</info>');
91 1
    }
92
93
    /**
94
     * Generate admin list classes.
95
     *
96
     * @param array $parameters
97
     */
98 1
    private function generateAdminLists(array $parameters)
99
    {
100 1
        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 1
    }
107
108
    /**
109
     * Generate the entity classes.
110
     *
111
     * @param array $parameters The template parameters
112
     */
113 1
    public function generateEntities(array $parameters)
114
    {
115 1
        $relPath = '/Entity/Pages/';
116 1
        $sourceDir = $this->skeletonDir.$relPath;
117 1
        $targetDir = $this->bundle->getPath().$relPath;
118
119 1
        $this->renderSingleFile($sourceDir, $targetDir, 'HomePage.php', $parameters);
120 1
        $this->renderSingleFile($sourceDir, $targetDir, 'ContentPage.php', $parameters);
121 1
        $this->renderSingleFile($sourceDir, $targetDir, 'BehatTestPage.php', $parameters);
122
123 1
        if ($this->demosite) {
124
            $this->renderSingleFile($sourceDir, $targetDir, 'FormPage.php', $parameters);
125
            $this->renderSingleFile($sourceDir, $targetDir, 'SearchPage.php', $parameters);
126
        }
127
128 1 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 1 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 1
        $this->assistant->writeLine('Generating entities : <info>OK</info>');
149 1
    }
150
151
    /**
152
     * Generate the form type classes.
153
     *
154
     * @param array $parameters The template parameters
155
     */
156 1
    public function generateFormTypes(array $parameters)
157
    {
158 1
        $relPath = '/Form/Pages/';
159 1
        $sourceDir = $this->skeletonDir.$relPath;
160 1
        $targetDir = $this->bundle->getPath().$relPath;
161
162 1
        $this->renderSingleFile($sourceDir, $targetDir, 'HomePageAdminType.php', $parameters);
163 1
        $this->renderSingleFile($sourceDir, $targetDir, 'ContentPageAdminType.php', $parameters);
164 1
        $this->renderSingleFile($sourceDir, $targetDir, 'BehatTestPageAdminType.php', $parameters);
165
166 1
        if ($this->demosite) {
167
            $this->renderSingleFile($sourceDir, $targetDir, 'FormPageAdminType.php', $parameters);
168
        }
169
170 1 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 1 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 1
        $this->assistant->writeLine('Generating form types : <info>OK</info>');
191 1
    }
192
193
    /**
194
     * Generate the menu adaptors classes.
195
     *
196
     * @param array $parameters The template parameters
197
     */
198 1
    public function generateMenuAdaptors(array $parameters)
199
    {
200 1
        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 1
    }
226
227
    /**
228
     * Generate the data fixtures classes.
229
     *
230
     * @param array $parameters The template parameters
231
     */
232 1
    public function generateFixtures(array $parameters)
233
    {
234 1
        $relPath = '/DataFixtures/ORM/DefaultSiteGenerator/';
235 1
        $sourceDir = $this->skeletonDir.$relPath;
236 1
        $targetDir = $this->bundle->getPath().$relPath;
237
238 1
        $this->renderSingleFile($sourceDir, $targetDir, 'DefaultSiteFixtures.php', $parameters);
239
240 1
        if ($this->demosite) {
241
            $this->renderSingleFile($sourceDir, $targetDir, 'SliderFixtures.php', $parameters);
242
            $this->renderSingleFile($sourceDir, $targetDir, 'SitemapFixtures.php', $parameters);
243
        }
244
245 1
        $this->assistant->writeLine('Generating fixtures : <info>OK</info>');
246 1
    }
247
248
    /**
249
     * Generate the pagepart section configuration.
250
     *
251
     * @param array $parameters The template parameters
252
     */
253 1
    public function generatePagepartConfigs(array $parameters)
254
    {
255 1
        $basePath = $this->isSymfony4() ? $this->container->getParameter('kernel.project_dir') : $this->bundle->getPath();
256 1
        $relPath = $this->isSymfony4() ? '/config/kunstmaancms/pageparts' : '/Resources/config/pageparts/';
257 1
        $sourceDir = $this->skeletonDir.'/Resources/config/pageparts/';
258 1
        $targetDir = $basePath.$relPath;
259
260 1
        $this->renderSingleFile($sourceDir, $targetDir, 'main.yml', $parameters);
261
262 1
        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 1
        $this->assistant->writeLine('Generating pagepart configuration : <info>OK</info>');
273 1
    }
274
275
    /**
276
     * Generate the page template configuration.
277
     *
278
     * @param array $parameters The template parameters
279
     */
280 1
    public function generatePagetemplateConfigs(array $parameters)
281
    {
282 1
        $basePath = $this->isSymfony4() ? $this->container->getParameter('kernel.project_dir') : $this->bundle->getPath();
283 1
        $relPath = $this->isSymfony4() ? '/config/kunstmaancms/pagetemplates/' : '/Resources/config/pagetemplates/';
284 1
        $sourceDir = $this->skeletonDir.'/Resources/config/pagetemplates/';
285 1
        $targetDir = $basePath.$relPath;
286
287 1
        $this->renderSingleFile($sourceDir, $targetDir, 'homepage.yml', $parameters);
288 1
        $this->renderSingleFile($sourceDir, $targetDir, 'contentpage.yml', $parameters);
289 1
        $this->renderSingleFile($sourceDir, $targetDir, 'behat-test-page.yml', $parameters);
290
291 1
        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 1
        $this->assistant->writeLine('Generating pagetemplate configuration : <info>OK</info>');
298 1
    }
299
300
    /**
301
     * Append to the application config file.
302
     */
303 1
    public function generateConfig()
304
    {
305 1
        if ($this->isSymfony4()) {
306 1
            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 1
    public function generateRouting(array $parameters)
326
    {
327 1
        if ($this->isSymfony4()) {
328 1
            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 1
    public function generateTemplates(array $parameters)
346
    {
347 1
        $relPath = '/Resources/views/Layout/';
348 1
        $sourceDir = $this->skeletonDir.$relPath;
349 1
        $targetDir = $this->getTemplateDir($this->bundle) . '/Layout/';
350
351 1
        if ($this->demosite) {
352
            $this->renderSingleFile($sourceDir, $targetDir, 'submenu.html.twig', $parameters);
353
        }
354
355
        // Pages
356
357 1
        $relPath = '/Resources/views/Pages/HomePage/';
358 1
        $sourceDir = $this->skeletonDir.$relPath;
359 1
        $targetDir = $this->getTemplateDir($this->bundle) . '/Pages/HomePage/';
360
361 1
        $this->renderSingleFile($sourceDir, $targetDir, 'pagetemplate.html.twig', $parameters);
362 1
        $this->renderSingleFile($sourceDir, $targetDir, 'view.html.twig', $parameters);
363
364 1
        $relPath = '/Resources/views/Pages/ContentPage/';
365 1
        $this->renderFiles($this->skeletonDir.$relPath, $this->getTemplateDir($this->bundle) . '/Pages/ContentPage/', $parameters, true);
366
367 1
        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 1
        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 1
        $relPath = '/Resources/views/Error/';
393 1
        $this->renderFiles($this->skeletonDir.$relPath, $this->getTemplateDir($this->bundle) . '/Error/', $parameters, true);
394
395 1
        $sourcePath = '/app/TwigBundle/';
396 1
        $targetPath = $this->isSymfony4() ? $this->rootDir . '/templates/bundles/TwigBundle' : $this->rootDir . '/app/Resources/TwigBundle/';
397 1
        $this->renderFiles($this->skeletonDir . $sourcePath, $targetPath, $parameters, true);
398
399
        // Bundle overwrites
400
401 1
        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 1
        $this->assistant->writeLine('Generating template files : <info>OK</info>');
413 1
    }
414
415
    /**
416
     * Generate the twig extensions.
417
     *
418
     * @param array $parameters The template parameters
419
     */
420 1
    public function generateTwigExtensions($parameters)
421
    {
422 1
        $relPath = '/Twig/';
423 1
        if ($this->demosite) {
424
            $this->renderSingleFile($this->skeletonDir.$relPath, $this->bundle->getPath().$relPath, 'BikesTwigExtension.php', $parameters, true);
425
        }
426
427 1
        if ($this->isSymfony4()) {
428 1
            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 1
    private function isMultiLangEnvironment()
443
    {
444
        // use the multilanguage parameter, if it exists
445 1
        if ($this->container->hasParameter('kunstmaan_admin.multi_language')) {
446 1
            return $this->container->getParameter('kunstmaan_admin.multi_language');
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