Completed
Push — master ( 95941d...273b09 )
by Peter
14:35 queued 12:22
created

Memcache::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2014, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Driver;
11
12
class Memcache extends BaseDriver
13
{
14
    /**
15
     * @var \Memcache
16
     */
17
    protected $memcache;
18
19
    /**
20
     * @var string
21
     */
22
    protected $prefix;
23
24
    /**
25
     * @param \Memcache $memcache
26
     * @param string $prefix
27
     */
28 6
    public function __construct(\Memcache $memcache, $prefix)
29
    {
30 6
        $this->memcache = $memcache;
31 6
        $this->prefix = $prefix;
32 6
    }
33
34
    /**
35
     * @param string $key
36
     *
37
     * @return \DateTime|null
38
     */
39 3
    public function get($key)
40
    {
41 3
        $key = $this->prefix.$key;
42 3
        if ($time = $this->memcache->get($key)) {
43 2
            return (new \DateTime())->setTimestamp($time);
44
        }
45
46 1
        return null;
47
    }
48
49
    /**
50
     * @param string $key
51
     * @param \DateTime $time
52
     *
53
     * @return bool
54
     */
55 1
    public function set($key, \DateTime $time)
56
    {
57 1
        $key = $this->prefix.$key;
58 1
        if (!($old_time = $this->memcache->get($key)) || $old_time < $time->getTimestamp()) {
59 1
            return $this->memcache->set($key, $time->getTimestamp());
60
        }
61
62 1
        return true;
63
    }
64
65
    /**
66
     * @param string $key
67
     *
68
     * @return bool
69
     */
70 1
    public function remove($key)
71
    {
72 1
        return $this->memcache->delete($this->prefix.$key);
73
    }
74
}
75