Completed
Push — master ( 64e7c0...1fbafa )
by Ryan
04:30 queued 54s
created

FileCache::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
namespace Opine\Cache;
3
4
class FileCache {
5
    private $root;
6
7
    private function getKeyFilePath (string $name):string
8
    {
9
        return $this->root . md5($name) . '.txt';
10
    }
11
12
    public function __construct (string $root)
13
    {
14
        $this->root = $root . '/var/cache/';
15
    }
16
17
    // just here for Memcache compatibilty
18
    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
        return true;
21
    }
22
23
    public function delete ($key):bool
24
    {
25
        $path = $this->getKeyFilePath($key);
26
        if (!file_exists($path)) {
27
            return false;
28
        }
29
        return unlink($path);
30
    }
31
32
    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
        $path = $this->getKeyFilePath($key);
35
        if (!is_string((string)$value)) {
36
            return false;
37
        }
38
        $value = (string)$value;
39
        file_put_contents($path, $value);
40
        return true;
41
    }
42
43
    public function get ($key)
44
    {
45
        if (is_array($key)) {
46
            $response = [];
47
            foreach ($key as $value) {
48
                $path = $this->getKeyFilePath($value);
49
                if (!file_exists($path)) {
50
                    $response[$value] = false;
51
                    continue;
52
                }
53
                $response[$value] = file_get_contents($path);
54
            }
55
            return $response;
56
        }
57
        $path = $this->getKeyFilePath($key);
58
        if (!file_exists($path)) {
59
            return false;
60
        }
61
        return file_get_contents($path);
62
    }
63
}
64