Completed
Push — master ( 7aa89c...4390df )
by Ryan
03:58
created

FileCache::getKeyFilePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace Opine\Cache;
3
4
class FileCache {
5
    private $root;
6
7 7
    private function getKeyFilePath (string $name):string
8
    {
9 7
        return $this->root . md5($name) . '.txt';
10
    }
11
12 8
    public function __construct (string $root)
13
    {
14 8
        $this->root = $root . '/var/cache/';
15 8
    }
16
17
    // just here for Memcache compatibilty
18 7
    public function pconnect($host, $port):bool
0 ignored issues
show
Unused Code introduced by
The parameter $host is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $port is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
19
    {
20 7
        return true;
21
    }
22
23 2
    public function delete ($key):bool
24
    {
25 2
        $path = $this->getKeyFilePath($key);
26 2
        if (!file_exists($path)) {
27
            return false;
28
        }
29 2
        return unlink($path);
30
    }
31
32 3
    public function set($key, $value, $flag, $expire):bool
0 ignored issues
show
Unused Code introduced by
The parameter $flag is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $expire is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
    {
34 3
        $path = $this->getKeyFilePath($key);
35 3
        if (!is_string((string)$value)) {
36
            return false;
37
        }
38 3
        $value = (string)$value;
39 3
        file_put_contents($path, $value);
40 3
        return true;
41
    }
42
43 6
    public function get ($key)
44
    {
45 6
        if (is_array($key)) {
46 2
            $response = [];
47 2
            foreach ($key as $value) {
48 2
                $path = $this->getKeyFilePath($value);
49 2
                if (!file_exists($path)) {
50 1
                    $response[$value] = false;
51 1
                    continue;
52
                }
53 2
                $response[$value] = file_get_contents($path);
54
            }
55 2
            return $response;
56
        }
57 4
        $path = $this->getKeyFilePath($key);
58 4
        if (!file_exists($path)) {
59 3
            return false;
60
        }
61 2
        return file_get_contents($path);
62
    }
63
}
64