Failed Conditions
Pull Request — experimental/3.1 (#2534)
by chihiro
09:59
created

ConfigManager::parsePluginConfigs()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 7.0753

Importance

Changes 0
Metric Value
cc 7
eloc 27
nc 16
nop 0
dl 0
loc 41
rs 6.7272
c 0
b 0
f 0
ccs 23
cts 26
cp 0.8846
crap 7.0753
1
<?php
2
3
namespace Eccube\Plugin;
4
5
use Symfony\Component\Finder\Finder;
6
use Symfony\Component\Yaml\Yaml;
7
8
/**
9
 * プラグインの config.yml, event.yml を扱うクラス
10
 *
11
 * TODO プラグインからこのクラスをカスタマイズすることはできないので,
12
        static メソッドにしているが, DI コンテナでの管理を検討する.
13
 */
14
class ConfigManager
15
{
16
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$debug" missing
Loading history...
17
     * すべてのプラグインの設定情報を返す.
18
     *
19
     * すべてのプラグインの config.yml 及び event.yml を読み込み、連想配列で返す.
20
     * キャッシュファイルが存在する場合は、キャッシュを利用する.
21
     * キャッシュファイルが存在しない場合は、キャッシュを生成する.
22
     * $app['debug'] = true の場合は、キャッシュを利用しない.
23
     *
24
     * @return array
25
     */
26 3
    public static function getPluginConfigAll($debug = false)
27
    {
28 3
        if ($debug) {
29 3
            return self::parsePluginConfigs();
30
        }
31
        $pluginConfigCache = self::getPluginConfigCacheFile();
32
        if (file_exists($pluginConfigCache)) {
33
            return require $pluginConfigCache;
34
        }
35
        if (self::writePluginConfigCache($pluginConfigCache) === false) {
36
            return self::parsePluginConfigs();
37
        } else {
38
            return require $pluginConfigCache;
39
        }
40
    }
41
42
    /**
43
     * プラグイン設定情報のキャッシュを書き込む.
44
     *
45
     * @param string $cacheFile
46
     * @return int|boolean file_put_contents() の結果
47
     */
48
    public static function writePluginConfigCache($cacheFile = null)
49
    {
50
        if (is_null($cacheFile)) {
51
            $cacheFile = self::getPluginConfigCacheFile();
52
        }
53
        $pluginConfigs = self::parsePluginConfigs();
54
        $temp_dir = self::getPluginTempRealDir();
55
        if (!file_exists($temp_dir)) {
56
            @mkdir($temp_dir);
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...
57
        }
58
        return file_put_contents($cacheFile, sprintf('<?php return %s', var_export($pluginConfigs, true)).';');
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
59
    }
60
61
    /**
62
     * プラグイン設定情報のキャッシュファイルを削除する.
63
     *
64
     * @return boolean
65
     */
66
    public static function removePluginConfigCache()
67
    {
68
        $cacheFile = self::getPluginConfigCacheFile();
69
        if (file_exists($cacheFile)) {
70
            return unlink($cacheFile);
71
        }
72
        return false;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
73
    }
74
75
    /**
76
     * プラグイン設定情報のキャッシュファイルパスを返す.
77
     *
78
     * @return string
79
     */
80
    public static function getPluginConfigCacheFile()
81
    {
82
        return self::getPluginTempRealDir().'/config_cache.php';
83
    }
84
85
    public static function getPluginTempRealDir()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
86
    {
87
        return __DIR__.'/../../../app/cache/plugin';
88
    }
89
90 3
    public static function getPluginRealDir()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
91
    {
92 3
        return __DIR__.'/../../../app/Plugin';
93
    }
94
95
    /**
96
     * プラグイン設定情報をパースし, 連想配列で返す.
97
     *
98
     * すべてのプラグインを探索し、 config.yml 及び event.yml をパースする.
99
     * パースした情報を連想配列で返す.
100
     *
101
     * @return array
102
     */
103 3
    public static function parsePluginConfigs()
104
    {
105
106 3
        $finder = Finder::create()
107 3
            ->in(self::getPluginRealDir())
108 3
            ->directories()
109 3
            ->depth(0);
110 3
        $finder->sortByName();
111
112 3
        $pluginConfigs = array();
113 3
        foreach ($finder as $dir) {
114 3
            $code = $dir->getBaseName();
115 3
            if (!$code) {
116
                //PHP5.3のgetBaseNameバグ対応
117
                if (PHP_VERSION_ID < 50400) {
118
                    $code = $dir->getFilename();
119
                }
120
            }
121 3
            $file = $dir->getRealPath().'/config.yml';
122 3
            $config = null;
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
123 3
            if (file_exists($file)) {
124 3
                $config = Yaml::parse(file_get_contents($file));
125
            } else {
126 3
                continue;
127
            }
128
129 3
            $file = $dir->getRealPath().'/event.yml';
130 3
            $event = null;
131 3
            if (file_exists($file)) {
132
                $event = Yaml::parse(file_get_contents($file));
133
            }
134 3
            if (!is_null($config)) {
135 3
                $pluginConfigs[$code] = array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
136 3
                    'config' => $config,
137 3
                    'event' => $event
138
                );
139
            }
140
        }
141
142 3
        return $pluginConfigs;
143
    }
144
}
145