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

Kunstmaan/AdminBundle/Composer/ScriptHandler.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
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