Completed
Push — master ( 3f1d1d...0341ae )
by Matze
05:15
created

FileCacheTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 46
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A includeFile() 0 9 2
A dumpCacheFile() 0 6 1
A dumpVariableToCache() 0 4 1
A getCacheFileName() 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 4
    protected function includeFile(string $name)
16
    {
17 4
        $filename = $this->getCacheFileName($name);
18 4
        if (!is_file($filename)) {
19
            return null;
20
        }
21
22 4
        return include $filename;
23
    }
24
25
    /**
26
     * @param string $name
27
     * @param string $content
28
     */
29 1
    protected function dumpCacheFile(string $name, string $content)
30
    {
31 1
        $fileName = $this->getCacheFileName($name);
32
33 1
        file_put_contents($fileName, "<?php \n//@codingStandardsIgnoreFile" . PHP_EOL . $content);
34 1
    }
35
36
    /**
37
     * @param string $name
38
     * @param mixed $variable
39
     */
40 1
    protected function dumpVariableToCache(string $name, $variable)
41
    {
42 1
        $this->dumpCacheFile($name, 'return ' . var_export($variable, true) . ';');
43 1
    }
44
45
    /**
46
     * @param string $name
47
     * @return string
48
     */
49 4
    private function getCacheFileName($name)
50
    {
51 4
        return ROOT . 'cache/' . basename($name, '.php')  . '.php';
52
    }
53
}
54