Passed
Push — master ( e05dd3...01a0a7 )
by Nils
02:57
created

Cache   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getGlobalKey() 0 3 1
A getFilename() 0 4 1
A __construct() 0 6 2
A set() 0 3 1
A get() 0 8 2
1
<?php
2
3
namespace Leankoala\HealthFoundation\Extenstion\Cache;
4
5
class Cache
6
{
7
    private $checkIdentifier;
8
9
    private $cacheDir = '/tmp/cache/';
10
11
    public function __construct($checkIdentifier)
12
    {
13
        if (!file_exists($this->cacheDir)) {
14
            mkdir($this->cacheDir, 0777, true);
15
        }
16
        $this->checkIdentifier = $checkIdentifier;
17
    }
18
19
    public function set($key, $value)
20
    {
21
        file_put_contents($this->getFilename($key), $value);
22
    }
23
24
    public function get($key)
25
    {
26
        $file = $this->getFilename($key);
27
28
        if (file_exists($file)) {
29
            return file_get_contents($file);
30
        } else {
31
            return null;
32
        }
33
    }
34
35
    private function getGlobalKey($key)
36
    {
37
        return $this->checkIdentifier . '_' . $key;
38
    }
39
40
    private function getFilename($key)
41
    {
42
        $globalKey = $this->getGlobalKey($key);
43
        return $this->cacheDir . md5($globalKey) . '.cache';
44
    }
45
}
46