Completed
Push — master ( e221c0...5a5b2e )
by Matze
05:22
created

FileCacheTrait::dumpVariableToCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace BrainExe\Core\Traits;
4
5
/**
6
 * @api
7
 */
8
trait FileCacheTrait
9
{
10
11
    /**
12
     * @param string $name
13
     * @return mixed
14
     */
15 5
    protected function includeFile(string $name)
16
    {
17 5
        $filename = $this->getCacheFileName($name);
18 5
        if (!is_file($filename)) {
19
            return null;
20
        }
21
22 5
        return include $filename;
23
    }
24
25
    /**
26
     * @param string $name
27
     * @param string $content
28
     */
29 2
    protected function dumpCacheFile(string $name, string $content)
30
    {
31 2
        $fileName = $this->getCacheFileName($name);
32
33 2
        file_put_contents($fileName, '<?php' . PHP_EOL . $content);
34 2
        @chmod($fileName, 0777);
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...
35 2
    }
36
37
    /**
38
     * @param string $name
39
     * @param mixed $variable
40
     */
41 2
    protected function dumpVariableToCache(string $name, $variable)
42
    {
43 2
        $this->dumpCacheFile($name, 'return ' . var_export($variable, true) . ';');
44 2
    }
45
46
    /**
47
     * @param string $name
48
     * @return string
49
     */
50 5
    private function getCacheFileName($name)
51
    {
52 5
        return ROOT . 'cache/' . basename($name, '.php')  . '.php';
53
    }
54
}
55