Completed
Pull Request — experimental/sf (#3393)
by chihiro
41:59
created

EccubeExtension::prepend()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 6.0442

Importance

Changes 0
Metric Value
cc 6
nc 10
nop 1
dl 0
loc 55
ccs 25
cts 28
cp 0.8929
crap 6.0442
rs 8.3595
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\DependencyInjection;
15
16
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Configuration as DoctrineBundleConfiguration;
17
use Doctrine\DBAL\Connection;
18
use Doctrine\DBAL\DriverManager;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
21
use Symfony\Component\Finder\Finder;
22
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
23
24
class EccubeExtension extends Extension implements PrependExtensionInterface
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
25
{
26
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$configs" missing
Loading history...
introduced by
Doc comment for parameter "$container" missing
Loading history...
27
     * Loads a specific configuration.
28
     *
29
     * @throws \InvalidArgumentException When provided tag is not defined in this extension
30
     */
31
    public function load(array $configs, ContainerBuilder $container)
32
    {
33
    }
34
35
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$container" missing
Loading history...
36
     * Allow an extension to prepend the extension configurations.
37
     */
38 1
    public function prepend(ContainerBuilder $container)
39
    {
40 1
        $pluginDir = $container->getParameter('kernel.project_dir').'/app/Plugin';
41 1
        $pluginDirs = $this->getPluginDirectories($pluginDir);
42
43 1
        $container->setParameter('eccube.plugins.enabled', []);
44
        // ファイル設置のみの場合は, 無効なプラグインとみなす.
45
        // DB接続後, 有効無効の判定を行う.
46 1
        $container->setParameter('eccube.plugins.disabled', $pluginDirs);
47
48
        // doctrine.yml, または他のprependで差し込まれたdoctrineの設定値を取得する.
49 1
        $configs = $container->getExtensionConfig('doctrine');
50
51
        // $configsは, env変数(%env(xxx)%)やパラメータ変数(%xxx.xxx%)がまだ解決されていないため, resolveEnvPlaceholders()で解決する
52
        // @see https://github.com/symfony/symfony/issues/22456
53 1
        $configs = $container->resolveEnvPlaceholders($configs, true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string|object<Symfony\Co...ncyInjection\true>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54
55
        // doctrine bundleのconfigurationで設定値を正規化する.
56 1
        $configration = new DoctrineBundleConfiguration($container->getParameter('kernel.debug'));
57 1
        $config = $this->processConfiguration($configration, $configs);
58
59
        // prependのタイミングではコンテナのインスタンスは利用できない.
60
        // 直接dbalのconnectionを生成し, dbアクセスを行う.
61 1
        $params = $config['dbal']['connections'][$config['dbal']['default_connection']];
62 1
        $conn = DriverManager::getConnection($params);
63
64 1
        if (!$this->isConnected($conn)) {
65
            return;
66
        }
67
68 1
        $stmt = $conn->query('select * from dtb_plugin');
69 1
        $plugins = $stmt->fetchAll();
70
71 1
        $enabled = [];
72 1
        foreach ($plugins as $plugin) {
73
            if ($plugin['enabled']) {
74
                $enabled[] = $plugin['code'];
75
            }
76
        }
77
78 1
        $disabled = [];
79 1
        foreach ($pluginDirs as $dir) {
80 1
            if (!in_array($dir, $enabled)) {
81 1
                $disabled[] = $dir;
82
            }
83
        }
84
85
        // 他で使いまわすため, パラメータで保持しておく.
86 1
        $container->setParameter('eccube.plugins.enabled', $enabled);
87 1
        $container->setParameter('eccube.plugins.disabled', $disabled);
88
89 1
        $pluginDir = $container->getParameter('kernel.project_dir').'/app/Plugin';
90 1
        $this->configureTwigPaths($container, $enabled, $pluginDir);
91 1
        $this->configureTranslations($container, $enabled, $pluginDir);
92
    }
93
94 1 View Code Duplication
    protected function configureTwigPaths(ContainerBuilder $container, $enabled, $pluginDir)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
95
    {
96 1
        $paths = [];
97
98 1
        foreach ($enabled as $code) {
99
            $dir = $pluginDir.'/'.$code.'/Resource/template';
100
            if (file_exists($dir)) {
101
                $paths[$dir] = $code;
102
            }
103
        }
104
105 1
        if (!empty($paths)) {
106
            $container->prependExtensionConfig('twig', [
107
                'paths' => $paths,
108
            ]);
109
        }
110
    }
111
112 1 View Code Duplication
    protected function configureTranslations(ContainerBuilder $container, $enabled, $pluginDir)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
113
    {
114 1
        $paths = [];
115
116 1
        foreach ($enabled as $code) {
117
            $dir = $pluginDir.'/'.$code.'/Resource/locale';
118
            if (file_exists($dir)) {
119
                $paths[] = $dir;
120
            }
121
        }
122
123 1
        if (!empty($paths)) {
124
            $container->prependExtensionConfig('framework', [
125
                'translator' => [
126
                    'paths' => $paths,
127
                ],
128
            ]);
129
        }
130
    }
131
132 1
    protected function isConnected(Connection $conn)
133
    {
134
        try {
135 1
            if (!$conn->ping()) {
136 1
                return false;
137
            }
138
        } catch (\Exception $e) {
139
            return false;
140
        }
141
142 1
        $sm = $conn->getSchemaManager();
143 1
        $tables = array_filter(
144 1
            $sm->listTables(),
145 1
            function ($table) {
146 1
                return $table->getName() === 'dtb_plugin';
147 1
            }
148
        );
149
150 1
        return empty($tables) ? false : true;
151
    }
152
153 1
    protected function getPluginDirectories($pluginDir)
154
    {
155 1
        $finder = (new Finder())
156 1
            ->in($pluginDir)
157 1
            ->sortByName()
158 1
            ->depth(0)
159 1
            ->directories();
160
161 1
        $dirs = [];
162 1
        foreach ($finder as $dir) {
163 1
            $dirs[] = $dir->getBaseName();
164
        }
165
166 1
        return $dirs;
167
    }
168
}
169