RedisLockerStore   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 128
Duplicated Lines 12.5 %

Importance

Changes 0
Metric Value
dl 16
loc 128
rs 10
c 0
b 0
f 0
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A acquire() 0 11 2
A clear() 0 3 1
A update() 0 13 2
A getAll() 0 3 1
A exists() 0 3 2
A delete() 7 7 2
A __construct() 0 3 1
A getLockPath() 0 3 1
A saveLock() 0 6 1
A get() 7 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file is part of the LockerManager package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LockerManager\Infrastructure;
12
13
use Cocur\Slugify\Slugify;
14
use LockerManager\Domain\Lock;
15
use LockerManager\Infrastructure\Exception\ExistingKeyException;
16
use LockerManager\Infrastructure\Exception\NotExistingKeyException;
17
use Predis\Client;
18
19
class RedisLockerStore implements LockerStoreInterface
20
{
21
    const LOCK_LIST_NAME = 'lock-list';
22
23
    /**
24
     * @var Client
25
     */
26
    private $redis;
27
28
    /**
29
     * RedisLockerStore constructor.
30
     * @param Client $redis
31
     */
32
    public function __construct(Client $redis)
33
    {
34
        $this->redis = $redis;
35
    }
36
37
    /**
38
     * @param Lock $lock
39
     * @throws ExistingKeyException
40
     */
41
    public function acquire(Lock $lock)
42
    {
43
        $key = $lock->key();
44
45
        if ($this->redis->hget(self::LOCK_LIST_NAME, $key)) {
46
            throw new ExistingKeyException(sprintf('The key "%s" already exists.', $key));
47
        }
48
49
        $this->saveLock(
50
            $key,
51
            $lock
52
        );
53
    }
54
55
    /**
56
     * @param $key
57
     * @return string
58
     */
59
    private function getLockPath($key)
60
    {
61
        return (new Slugify())->slugify($key);
62
    }
63
64
    /**
65
     * @param $key
66
     * @param Lock $lock
67
     */
68
    private function saveLock($key, Lock $lock)
69
    {
70
        $this->redis->hset(
71
            self::LOCK_LIST_NAME,
72
            $this->getLockPath($key),
73
            serialize($lock)
74
        );
75
    }
76
77
    /**
78
     * clear all locks
79
     */
80
    public function clear()
81
    {
82
        $this->redis->del([self::LOCK_LIST_NAME]);
83
    }
84
85
    /**
86
     * @param $key
87
     * @throws NotExistingKeyException
88
     */
89 View Code Duplication
    public function delete($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        if (!$this->exists($key)) {
92
            throw new NotExistingKeyException(sprintf('The key "%s" does not exists.', $key));
93
        }
94
95
        $this->redis->hdel(self::LOCK_LIST_NAME, $this->getLockPath($key));
96
    }
97
98
    /**
99
     * @param $key
100
     * @return bool
101
     */
102
    public function exists($key)
103
    {
104
        return ($this->redis->hget(self::LOCK_LIST_NAME, $this->getLockPath($key))) ? true : false;
105
    }
106
107
    /**
108
     * @param $key
109
     * @return mixed
110
     * @throws NotExistingKeyException
111
     */
112 View Code Duplication
    public function get($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
    {
114
        if (!$this->exists($key)) {
115
            throw new NotExistingKeyException(sprintf('The key "%s" does not exists.', $key));
116
        }
117
118
        return unserialize($this->redis->hget(self::LOCK_LIST_NAME, $this->getLockPath($key)));
119
    }
120
121
    /**
122
     * @return array
123
     */
124
    public function getAll()
125
    {
126
        return $this->redis->hgetall(self::LOCK_LIST_NAME);
127
    }
128
129
    /**
130
     * @param $key
131
     * @param $payload
132
     * @throws NotExistingKeyException
133
     */
134
    public function update($key, $payload)
135
    {
136
        if (!$this->redis->hget(self::LOCK_LIST_NAME, $this->getLockPath($key))) {
137
            throw new NotExistingKeyException(sprintf('The key "%s" does not exists.', $key));
138
        }
139
140
        /** @var Lock $lock */
141
        $lock = $this->get($key);
142
        $lock->update($payload);
143
144
        $this->saveLock(
145
            $key,
146
            $lock
147
        );
148
    }
149
}
150