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

FileCacheTrait   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 66.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 46
ccs 10
cts 15
cp 0.6667
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A includeFile() 0 8 2
A dumpCacheFile() 0 7 1
A dumpVariableToCache() 0 4 1
A getCacheFileName() 0 4 1
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