Passed
Push — master ( d45b2a...c32bfc )
by Krisztián
02:16
created

Bucket::getFromBackup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpCache\Storage;
4
5
/**
6
 * Description of MessageBucket.
7
 *
8
 * @author dude920228
9
 */
10
class Bucket implements StorageInterface
11
{
12
    private $entries;
13
    private $backupDir;
14
    
15
    public function __construct($backupDir)
16
    {
17
        $this->backupDir = $backupDir;
18
        $this->entries = [];
19
    }
20
21
    public function get($key)
22
    {
23
        if (!array_key_exists($key, $this->entries) && !$this->existsInBackup($key)) {
24
            return false;
25
        }
26
        if($this->existsInBackup($key)) {
27
            $entry = unserialize($this->getFromBackup($key));
28
            return gzuncompress($entry['content']);
29
        }
30
        return gzuncompress($this->entries[$key]['content']);
31
    }
32
    
33
    private function existsInBackup($key)
34
    {
35
        if(file_exists($this->backupDir."/".$key.".dat")) {
36
            return true;
37
        }
38
        return false;
39
    }
40
    
41
    private function getFromBackup($key)
42
    {
43
        $contents = "";
44
        $handle = fopen($this->backupDir."/".$key.".dat", "r+");
45
        while(!feof($handle)) {
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of feof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        while(!feof(/** @scrutinizer ignore-type */ $handle)) {
Loading history...
46
            $contents .= fread($handle, 32);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
            $contents .= fread(/** @scrutinizer ignore-type */ $handle, 32);
Loading history...
47
        }
48
        return $contents;
49
    }
50
    
51
    public function store($key, $entry, $time = null)
52
    {
53
        $compressed = gzcompress($entry, 9);
54
        $this->entries[$key]['content'] = $compressed;
55
        $this->entries[$key]['created_time'] = is_null($time) ? time() : $time;
56
        if (!$compressed) {
57
            return false;
58
        }
59
60
        return true;
61
    }
62
63
    public function getEntries()
64
    {
65
        return $this->entries;
66
    }
67
68
    public function delete($key)
69
    {
70
        if (array_key_exists($key, $this->entries)) {
71
            unset($this->entries[$key]);
72
73
            return true;
74
        }
75
76
        return false;
77
    }
78
79
    public function getKeys()
80
    {
81
        return array_keys($this->entries);
82
    }
83
}
84