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

Cache::getFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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