Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
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
@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);
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 = [
18
        'symfony-app-dir' => 'app',
19
        'multi-language-option' => 'multilanguage',
20
    ];
21
22
    public static function checkMultiLangConfigs(Event $event)
23
    {
24
        $options = static::getOptions($event);
25
        $appDir = getcwd() . '/' . $options['symfony-app-dir'];
26
        $configDir = $appDir . '/config';
27
        $parametersFile = $configDir . '/parameters.yml';
28
        $routingFile = $configDir . '/routing.yml';
29
        $securityFile = $configDir . '/security.yml';
30
        $singleLangRoutingFile = $configDir . '/routing.singlelang.yml';
31
        $singleLangSecurityFile = $configDir . '/security.singlelang.yml';
32
        $multiLangRoutingFile = $configDir . '/routing.multilang.yml';
33
        $multiLangSecurityFile = $configDir . '/security.multilang.yml';
34
35
        if (is_file($parametersFile)) {
36
            $parameters = self::getConfigParameters($parametersFile);
37
38
            if (isset($parameters[$options['multi-language-option']])) {
39
                $multiLanguage = $parameters[$options['multi-language-option']];
40
                if (!$multiLanguage) {
41
                    $fs = new Filesystem();
42
43
                    // move routing
44 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...
45
                        try {
46
                            $fs->rename($routingFile, $multiLangRoutingFile);
47
                            $fs->rename($singleLangRoutingFile, $routingFile);
48
                            $event->getIO()->write(sprintf('Replaced routing config with single language config'));
49
                        } catch (IOException $ioE) {
50
                            $event->getIO()->write(sprintf('Exception while moving routing file to singlelang routing file: <error>%s</error>', $ioE->getMessage()));
51
                        }
52
                    }
53
                    // move security
54 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...
55
                        try {
56
                            $fs->rename($securityFile, $multiLangSecurityFile);
57
                            $fs->rename($singleLangSecurityFile, $securityFile);
58
                            $event->getIO()->write(sprintf('Replaced security config with single language config'));
59
                        } catch (IOException $ioE) {
60
                            $event->getIO()->write(sprintf('Exception while moving routing file to singlelang routing file: <error>%s</error>', $ioE->getMessage()));
61
                        }
62
                    }
63
                }
64
            }
65
        }
66
    }
67
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'] : [];
87
    }
88
}
89