ReflectionCacheApc::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
namespace Atreyu;
4
5
class ReflectionCacheApc implements ReflectionCache
6
{
7
    private $localCache;
8
    private $timeToLive = 5;
9
10
    public function __construct(ReflectionCache $localCache = null)
11
    {
12
        $this->localCache = $localCache ?: new ReflectionCacheArray;
13
    }
14
15
    public function setTimeToLive($seconds)
16
    {
17
        $seconds = (int) $seconds;
18
        $this->timeToLive = ($seconds > 0) ? $seconds : $this->timeToLive;
19
20
        return $this;
21
    }
22
23
    public function fetch($key)
24
    {
25
        $localData = $this->localCache->fetch($key);
26
27
        if ($localData != false) {
28
            return $localData;
29
        } else {
30
            $success = null; // stupid by-ref parameter that scrutinizer complains about
31
            $data = apc_fetch($key, $success);
32
            return $success ? $data : false;
33
        }
34
    }
35
36
    public function store($key, $data)
37
    {
38
        $this->localCache->store($key, $data);
39
        apc_store($key, $data, $this->timeToLive);
40
    }
41
}
42