Completed
Push — master ( 6e12de...23243d )
by Peter
03:07
created

Memcached::set()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

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