Completed
Push — master ( 5fa5de...689521 )
by Matze
03:31
created

FileCacheTrait::includeFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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