Completed
Push — master ( 13039a...06c1ce )
by Jeroen
07:15
created

ScriptHandler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 79
Duplicated Lines 22.78 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 18
loc 79
ccs 0
cts 50
cp 0
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 7 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
3
namespace Kunstmaan\AdminBundle\Composer;
4
5
use Composer\Script\Event;
6
use Symfony\Component\Filesystem\Exception\IOException;
7
use Symfony\Component\Filesystem\Filesystem;
8
use Symfony\Component\Yaml\Parser;
9
10
@trigger_error(sprintf('The composer script class "%s" is deprecated in KunstmaanAdminBundle 5.4 and will be removed in KunstmaanAdminBundle 6.0. If you use this script handler, remove it from your composer.json scripts section.', __CLASS__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
11
12
/**
13
 * NEXT_MAJOR remove the symfony/filesystem and symfony/yaml as direct dependency of the admin-bundle (if unused in other classes)
14
 */
15
class ScriptHandler
16
{
17
    protected static $options = array(
18
        'symfony-app-dir' => 'app',
19
        'multi-language-option' => 'multilanguage',
20
    );
21
22
    /**
23
     * @param Event $event
24
     */
25
    public static function checkMultiLangConfigs(Event $event)
26
    {
27
        $options = static::getOptions($event);
28
        $appDir = getcwd() . '/' . $options['symfony-app-dir'];
29
        $configDir = $appDir . '/config';
30
        $parametersFile = $configDir . '/parameters.yml';
31
        $routingFile = $configDir . '/routing.yml';
32
        $securityFile = $configDir . '/security.yml';
33
        $singleLangRoutingFile = $configDir . '/routing.singlelang.yml';
34
        $singleLangSecurityFile = $configDir . '/security.singlelang.yml';
35
        $multiLangRoutingFile = $configDir . '/routing.multilang.yml';
36
        $multiLangSecurityFile = $configDir . '/security.multilang.yml';
37
38
        if (is_file($parametersFile)) {
39
            $parameters = self::getConfigParameters($parametersFile);
40
41
            if (isset($parameters[$options['multi-language-option']])) {
42
                $multiLanguage = $parameters[$options['multi-language-option']];
43
                if (!$multiLanguage) {
44
                    $fs = new Filesystem();
45
46
                    // move routing
47 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...
48
                        try {
49
                            $fs->rename($routingFile, $multiLangRoutingFile);
50
                            $fs->rename($singleLangRoutingFile, $routingFile);
51
                            $event->getIO()->write(sprintf('Replaced routing config with single language config'));
52
                        } catch (IOException $ioE) {
53
                            $event->getIO()->write(sprintf('Exception while moving routing file to singlelang routing file: <error>%s</error>', $ioE->getMessage()));
54
                        }
55
                    }
56
                    // move security
57 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...
58
                        try {
59
                            $fs->rename($securityFile, $multiLangSecurityFile);
60
                            $fs->rename($singleLangSecurityFile, $securityFile);
61
                            $event->getIO()->write(sprintf('Replaced security config with single language config'));
62
                        } catch (IOException $ioE) {
63
                            $event->getIO()->write(sprintf('Exception while moving routing file to singlelang routing file: <error>%s</error>', $ioE->getMessage()));
64
                        }
65
                    }
66
                }
67
            }
68
        }
69
    }
70
71
    /**
72
     * @param Event $event
73
     *
74
     * @return array
75
     */
76
    protected static function getOptions(Event $event)
77
    {
78
        return array_merge(static::$options, $event->getComposer()->getPackage()->getExtra());
79
    }
80
81
    /**
82
     * @param string $parametersFile
83
     *
84
     * @return array
85
     */
86
    protected static function getConfigParameters($parametersFile)
87
    {
88
        $ymlParser = new Parser();
89
        $config = $ymlParser->parse(file_get_contents($parametersFile));
90
91
        return isset($config['parameters']) ? $config['parameters'] : array();
92
    }
93
}
94