Completed
Push — master ( 68b501...8e644d )
by Loban
01:50
created

PhpFileStorage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 40
ccs 11
cts 14
cp 0.7856
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 8 2
A setValue() 0 14 4
1
<?php
2
/**
3
 * @link https://github.com/LAV45/yii2-settings
4
 * @copyright Copyright (c) 2016 LAV45
5
 * @author Alexey Loban <[email protected]>
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace lav45\settings\storage;
10
11
use yii\helpers\VarDumper;
12
13
class PhpFileStorage extends FileStorage
14
{
15
    /**
16
     * @var string settings file suffix. Defaults to '.php'.
17
     */
18
    public $fileSuffix = '.php';
19
20
    /**
21
     * @param string $key
22
     * @return mixed
23
     */
24 3
    public function getValue($key)
25
    {
26 3
        $file = $this->getFile($key);
27 3
        if (file_exists($file)) {
28 3
            return include $file;
29
        }
30 3
        return false;
31
    }
32
33
    /**
34
     * @param string $key
35
     * @param mixed $value
36
     * @return bool
37
     */
38 3
    public function setValue($key, $value)
39
    {
40 3
        $value = is_string($value) ? "'{$value}'" : VarDumper::export($value);
41 3
        $value = "<?php\nreturn {$value};\n";
42
43 3
        $result = parent::setValue($key, $value);
44
45 3
        if (function_exists('opcache_compile_file') && ini_get('opcache.enable')) {
46
            $file = $this->getFile($key);
47
            @opcache_compile_file($file);
1 ignored issue
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...
48
        }
49
50 3
        return $result;
51
    }
52
}