DoctrineCacheAdapter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 86.36%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 61
ccs 19
cts 22
cp 0.8636
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setValue() 0 4 1
A incrementValue() 0 4 1
A getValue() 0 7 2
A setTimestamp() 0 4 1
A getTimestamp() 0 7 2
A expire() 0 5 1
A expireIn() 0 4 1
1
<?php
2
namespace BehEh\Flaps\Storage;
3
4
use BehEh\Flaps\StorageInterface;
5
use Doctrine\Common\Cache\Cache;
6
7
/**
8
 * Provides a storage adapter using a Doctrine\Common\Cache\Cache implementation as backend.
9
 *
10
 * Example using Doctrine\Common\Cache\ApcCache:
11
 * <pre><code>
12
 * <?php
13
 * use Doctrine\Common\Cache\ApcCache;
14
 * use BehEh\Flaps\Storage\DoctrineCacheAdapter;
15
 * 
16
 * $apc = new ApcCache();
17
 * $apc->setNamespace('MyApplication');
18
 * $storage = new DoctrineCacheAdapter($apc);
19
 * </pre></code>
20
 *
21
 * @since 0.1
22
 * @author Benedict Etzel <[email protected]>
23
 */
24
class DoctrineCacheAdapter implements StorageInterface
25
{
26
    /**
27
     * @var Cache
28
     */
29
    protected $cache;
30
31
    /**
32
     * Sets up the adapter using the Doctrine cache implementation $cache.
33
     * @param Doctrine\Common\Cache\Cache $cache the cache implementation to use as storage backend
34
     */
35
    public function __construct(Cache $cache)
36
    {
37
        $this->cache = $cache;
38
    }
39
40 1
    public function setValue($key, $value)
41
    {
42 1
        $this->cache->save($key, intval($value));
43 1
    }
44
45 1
    public function incrementValue($key)
46
    {
47 1
        throw new \Exception('incrementValue() is not implemented for DoctrineCacheAdapter');
48
    }
49
50 1
    public function getValue($key)
51
    {
52 1
        if (!$this->cache->contains($key)) {
53 1
            return 0;
54
        }
55 1
        return intval($this->cache->fetch($key));
56
    }
57
58 1
    public function setTimestamp($key, $timestamp)
59
    {
60 1
        $this->cache->save($key.':timestamp', floatval($timestamp));
61 1
    }
62
63 1
    public function getTimestamp($key)
64
    {
65 1
        if (!$this->cache->contains($key.':timestamp')) {
66 1
            return (float) 0;
67
        }
68 1
        return floatval($this->cache->fetch($key.':timestamp'));
69
    }
70
71 3
    public function expire($key)
72
    {
73 3
        $this->cache->delete($key.':timestamp');
74 3
        return $this->cache->delete($key);
75
    }
76
77
    /**
78
     * @codeCoverageIgnore
79
     */
80
    public function expireIn($key, $seconds)
81
    {
82
        return false;
83
    }
84
}
85