Shmop   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 70
c 1
b 0
f 0
ccs 19
cts 19
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 9 2
A set() 0 9 3
A remove() 0 6 1
A getIdByKey() 0 4 1
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
use AnimeDb\Shmop\FixedBlock as BlockShmop;
13
14
class Shmop extends BaseDriver
15
{
16
    /**
17
     * Cache key salt.
18
     *
19
     * @var string
20
     */
21
    protected $salt;
22
23
    /**
24
     * @param string $salt
25
     */
26 8
    public function __construct($salt)
27
    {
28 8
        $this->salt = $salt;
29 8
    }
30
31
    /**
32
     * @param string $key
33
     *
34
     * @return \DateTime|null
35
     */
36 5
    public function get($key)
37
    {
38 5
        $sh = new BlockShmop($this->getIdByKey($key), 10);
39 5
        if ($time = $sh->read()) {
40 3
            return (new \DateTime())->setTimestamp($time);
41
        }
42
43 2
        return;
44
    }
45
46
    /**
47
     * @param string $key
48
     * @param \DateTime $time
49
     *
50
     * @return bool
51
     */
52 5
    public function set($key, \DateTime $time)
53
    {
54 5
        $sh = new BlockShmop($this->getIdByKey($key), 10);
55 5
        if (!($old_time = $sh->read()) || $old_time < $time->getTimestamp()) {
56 5
            $sh->write($time->getTimestamp());
57 5
        }
58
59 5
        return true;
60
    }
61
62
    /**
63
     * @param string $key
64
     *
65
     * @return bool
66
     */
67 1
    public function remove($key)
68
    {
69 1
        $sh = new BlockShmop($this->getIdByKey($key), 10);
70
71 1
        return $sh->delete();
72
    }
73
74
    /**
75
     * @param string $key
76
     *
77
     * @return int
78
     */
79 8
    public function getIdByKey($key)
80
    {
81 8
        return (int) sprintf('%u', crc32($key.$this->salt));
82
    }
83
}
84