Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

ScriptHandler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 76
Duplicated Lines 23.68 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 18
loc 76
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B checkMultiLangConfigs() 18 45 10
A getOptions() 0 4 1
A getConfigParameters() 0 6 2

How to fix   Duplicated Code   

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:

1
<?php
2
namespace Kunstmaan\AdminBundle\Composer;
3
4
use Composer\Script\Event;
5
use Symfony\Component\Filesystem\Exception\IOException;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Symfony\Component\Yaml\Parser;
8
9
class ScriptHandler
10
{
11
    protected static $options = array(
12
        'symfony-app-dir' => 'app',
13
        'multi-language-option' => 'multilanguage'
14
    );
15
16
    /**
17
     * @param Event $event
18
     */
19
    public static function checkMultiLangConfigs(Event $event)
20
    {
21
        $options = static::getOptions($event);
22
        $appDir = getcwd() . '/' . $options['symfony-app-dir'];
23
        $configDir = $appDir . '/config';
24
        $parametersFile = $configDir . '/parameters.yml';
25
        $routingFile = $configDir . '/routing.yml';
26
        $securityFile = $configDir . '/security.yml';
27
        $singleLangRoutingFile = $configDir . '/routing.singlelang.yml';
28
        $singleLangSecurityFile = $configDir . '/security.singlelang.yml';
29
        $multiLangRoutingFile = $configDir . '/routing.multilang.yml';
30
        $multiLangSecurityFile = $configDir . '/security.multilang.yml';
31
32
        if (is_file($parametersFile)) {
33
            $parameters = self::getConfigParameters($parametersFile);
34
35
            if (isset($parameters[$options['multi-language-option']])) {
36
                $multiLanguage = $parameters[$options['multi-language-option']];
37
                if (! $multiLanguage) {
38
                    $fs = new Filesystem();
39
40
                    // move routing
41 View Code Duplication
                    if (is_file($singleLangRoutingFile) && ! is_file($multiLangRoutingFile)) {
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...
42
                        try {
43
                            $fs->rename($routingFile, $multiLangRoutingFile);
44
                            $fs->rename($singleLangRoutingFile, $routingFile);
45
                            $event->getIO()->write(sprintf("Replaced routing config with single language config"));
46
                        } catch (IOException $ioE) {
47
                            $event->getIO()->write(sprintf('Exception while moving routing file to singlelang routing file: <error>%s</error>', $ioE->getMessage()));
48
                        }
49
                    }
50
                    // move security
51 View Code Duplication
                    if (is_file($singleLangSecurityFile) && ! is_file($multiLangSecurityFile)) {
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...
52
                        try {
53
                            $fs->rename($securityFile, $multiLangSecurityFile);
54
                            $fs->rename($singleLangSecurityFile, $securityFile);
55
                            $event->getIO()->write(sprintf("Replaced security config with single language config"));
56
                        } catch (IOException $ioE) {
57
                            $event->getIO()->write(sprintf('Exception while moving routing file to singlelang routing file: <error>%s</error>', $ioE->getMessage()));
58
                        }
59
                    }
60
                }
61
            }
62
        }
63
    }
64
65
    /**
66
     * @param Event $event
67
     * @return array
68
     */
69
    protected static function getOptions(Event $event)
70
    {
71
        return array_merge(static::$options, $event->getComposer()->getPackage()->getExtra());
72
    }
73
74
    /**
75
     * @param string $parametersFile
76
     * @return array
77
     */
78
    protected static function getConfigParameters($parametersFile)
79
    {
80
        $ymlParser = new Parser();
81
        $config = $ymlParser->parse(file_get_contents($parametersFile));
82
        return isset($config['parameters']) ? $config['parameters'] : array();
83
    }
84
}
85