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