UberMemcached   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 106
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 4 1
A getMemcached() 0 4 1
A setConnection() 0 4 1
A getItem() 0 4 1
A deleteItem() 0 4 1
A dropCache() 0 4 1
A isFresh() 0 3 1
A getResource() 0 3 1
A addItem() 0 8 2
A getAllKeys() 0 13 3
1
<?php
2
3
namespace Sleepness\UberTranslationBundle\Storage;
4
5
use \Memcached;
6
use Symfony\Component\Config\Resource\ResourceInterface;
7
8
/**
9
 * Wrapper under standard Memcached class, which ease work with memcached
10
 *
11
 * @author Viktor Novikov <[email protected]>
12
 * @author Alexandr Zhulev <[email protected]>
13
 */
14
class UberMemcached implements ResourceInterface, UberStorageInterface
15
{
16
    /**
17
     * @var Memcached $memcached
18
     */
19
    private $memcached;
20
21
    /**
22
     * @param Memcached $memcached
23
     */
24
    public function __construct(Memcached $memcached)
25
    {
26
        $this->memcached = $memcached;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function __toString()
33
    {
34
        return 'uberMemcached';
35
    }
36
37
    /**
38
     * @return Memcached
39
     */
40
    public function getMemcached()
41
    {
42
        return $this->memcached;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function setConnection($host, $port)
49
    {
50
        $this->getMemcached()->addServer($host, $port);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function addItem($key, $value, $expiration = null)
57
    {
58
        if ($expiration === null) {
59
            $expiration = 60 * 60 * 24 * 30; // default expires after 30 days
60
        }
61
62
        return $this->getMemcached()->set($key, $value, $expiration);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getItem($key)
69
    {
70
        return $this->getMemcached()->get($key);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getAllKeys()
77
    {
78
        $allKeys = $this->getMemcached()->getAllKeys();
79
        $locales = array();
80
        foreach ($allKeys as $key) {
81
            if (!preg_match('/^[a-z]{2}_[a-zA-Z]{2}$|[a-z]{2}/', $key)) {
82
                continue;
83
            }
84
            $locales[] = $key;
85
        }
86
87
        return $locales;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function deleteItem($key)
94
    {
95
        return $this->getMemcached()->delete($key);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function dropCache()
102
    {
103
        return $this->getMemcached()->flush(0);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function isFresh($timestamp)
110
    {
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function getResource()
117
    {
118
    }
119
}
120