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

FileCacheTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 93.33%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
c 3
b 0
f 0
lcom 1
cbo 0
dl 0
loc 47
ccs 14
cts 15
cp 0.9333
rs 10

4 Methods

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