Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

Kunstmaan/AdminBundle/Composer/ScriptHandler.php (4 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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 15 and the first side effect is on line 10.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
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
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