Passed
Push — master ( b2988e...9f380c )
by Jonathan
33:05
created

ArrayCache::doGetStats()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
ccs 2
cts 2
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Common\Cache;
4
5
use function time;
6
7
/**
8
 * Array cache driver.
9
 *
10
 * @link   www.doctrine-project.org
11
 */
12
class ArrayCache extends CacheProvider
13
{
14
    /** @var array[] $data each element being a tuple of [$data, $expiration], where the expiration is int|bool */
15
    private $data = [];
16
17
    /** @var int */
18
    private $hitsCount = 0;
19
20
    /** @var int */
21
    private $missesCount = 0;
22
23
    /** @var int */
24
    private $upTime;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function __construct()
30
    {
31
        $this->upTime = time();
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected function doFetch($id)
38
    {
39
        if (! $this->doContains($id)) {
40
            $this->missesCount += 1;
41
42
            return false;
43
        }
44
45
        $this->hitsCount += 1;
46
47
        return $this->data[$id][0];
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function doContains($id)
54
    {
55
        if (! isset($this->data[$id])) {
56
            return false;
57
        }
58 157
59
        $expiration = $this->data[$id][1];
60 157
61 157
        if ($expiration && $expiration < time()) {
62
            $this->doDelete($id);
63
64
            return false;
65
        }
66 142
67
        return true;
68 142
    }
69 78
70
    /**
71 78
     * {@inheritdoc}
72
     */
73
    protected function doSave($id, $data, $lifeTime = 0)
74 131
    {
75
        $this->data[$id] = [$data, $lifeTime ? time() + $lifeTime : false];
76 131
77
        return true;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82 153
     */
83
    protected function doDelete($id)
84 153
    {
85 153
        unset($this->data[$id]);
86
87
        return true;
88 141
    }
89
90 141
    /**
91 2
     * {@inheritdoc}
92
     */
93 2
    protected function doFlush()
94
    {
95
        $this->data = [];
96 141
97
        return true;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102 149
     */
103
    protected function doGetStats()
104 149
    {
105
        return [
106 149
            Cache::STATS_HITS             => $this->hitsCount,
107
            Cache::STATS_MISSES           => $this->missesCount,
108
            Cache::STATS_UPTIME           => $this->upTime,
109
            Cache::STATS_MEMORY_USAGE     => null,
110
            Cache::STATS_MEMORY_AVAILABLE => null,
111
        ];
112 93
    }
113
}
114