RiakStore   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 214
Duplicated Lines 15.89 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 14
Bugs 1 Features 5
Metric Value
wmc 32
c 14
b 1
f 5
lcom 1
cbo 8
dl 34
loc 214
ccs 84
cts 84
cp 1
rs 9.6

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A set() 0 12 2
A get() 17 17 3
A getOrFail() 17 17 3
B getMultiple() 0 26 6
B getMultipleOrFail() 0 31 6
A remove() 0 18 3
A exists() 0 10 2
A clear() 0 12 3
A keys() 0 8 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
/*
4
 * This file is part of the webmozart/key-value-store package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\KeyValueStore;
13
14
use Basho\Riak\Riak;
15
use Exception;
16
use Webmozart\KeyValueStore\Api\KeyValueStore;
17
use Webmozart\KeyValueStore\Api\NoSuchKeyException;
18
use Webmozart\KeyValueStore\Api\ReadException;
19
use Webmozart\KeyValueStore\Api\WriteException;
20
use Webmozart\KeyValueStore\Util\KeyUtil;
21
use Webmozart\KeyValueStore\Util\Serializer;
22
23
/**
24
 * A key-value store backed by a Riak client.
25
 *
26
 * @since  1.0
27
 *
28
 * @author Bernhard Schussek <[email protected]>
29
 */
30
class RiakStore implements KeyValueStore
31
{
32
    /**
33
     * @var string
34
     */
35
    private $bucketName;
36
37
    /**
38
     * @var Riak
39
     */
40
    private $client;
41
42
    /**
43
     * Creates a store backed by a Riak client.
44
     *
45
     * If no client is passed, a new one is created using the default server
46
     * "127.0.0.1" and the default port 8098.
47
     *
48
     * @param string    $bucketName The name of the Riak bucket to use.
49
     * @param Riak|null $client     The client used to connect to Riak.
50
     */
51 90
    public function __construct($bucketName, Riak $client = null)
52
    {
53 90
        $this->bucketName = $bucketName;
54 90
        $this->client = $client ?: new Riak();
55 90
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 56
    public function set($key, $value)
61
    {
62 56
        KeyUtil::validate($key);
63
64 54
        $serialized = Serializer::serialize($value);
65
66
        try {
67 52
            $this->client->bucket($this->bucketName)->newBinary($key, $serialized)->store();
0 ignored issues
show
Documentation introduced by
$serialized is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68 1
        } catch (Exception $e) {
69 1
            throw WriteException::forException($e);
70
        }
71 51
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 8 View Code Duplication
    public function get($key, $default = null)
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...
77
    {
78 8
        KeyUtil::validate($key);
79
80
        try {
81 6
            $object = $this->client->bucket($this->bucketName)->getBinary($key);
82 5
            $exists = $object->exists();
83 2
        } catch (Exception $e) {
84 2
            throw ReadException::forException($e);
85
        }
86
87 4
        if (!$exists) {
88 1
            return $default;
89
        }
90
91 3
        return Serializer::unserialize($object->getData());
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 33 View Code Duplication
    public function getOrFail($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...
98
    {
99 33
        KeyUtil::validate($key);
100
101
        try {
102 31
            $object = $this->client->bucket($this->bucketName)->getBinary($key);
103 30
            $exists = $object->exists();
104 2
        } catch (Exception $e) {
105 2
            throw ReadException::forException($e);
106
        }
107
108 29
        if (!$exists) {
109 1
            throw NoSuchKeyException::forKey($key);
110
        }
111
112 28
        return Serializer::unserialize($object->getData());
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 7
    public function getMultiple(array $keys, $default = null)
119
    {
120 7
        KeyUtil::validateMultiple($keys);
121
122 5
        $values = array();
123
124
        try {
125 5
            $bucket = $this->client->bucket($this->bucketName);
126
127 4
            foreach ($keys as $key) {
128 4
                $object = $bucket->getBinary($key);
129
130 4
                $values[$key] = $object->exists() ? $object->getData() : false;
131
            }
132 2
        } catch (Exception $e) {
133 2
            throw ReadException::forException($e);
134
        }
135
136 3
        foreach ($values as $key => $value) {
137 3
            $values[$key] = false === $value
138 1
                ? $default
139 3
                : Serializer::unserialize($value);
140
        }
141
142 2
        return $values;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 33
    public function getMultipleOrFail(array $keys)
149
    {
150 33
        KeyUtil::validateMultiple($keys);
151
152 31
        $values = array();
153 31
        $notFoundKeys = array();
154
155
        try {
156 31
            $bucket = $this->client->bucket($this->bucketName);
157
158 30
            foreach ($keys as $key) {
159 30
                $values[$key] = $bucket->getBinary($key);
160
161 30
                if (!$values[$key]->exists()) {
162 29
                    $notFoundKeys[] = $key;
163
                }
164
            }
165 2
        } catch (Exception $e) {
166 2
            throw ReadException::forException($e);
167
        }
168
169 29
        if (0 !== count($notFoundKeys)) {
170 1
            throw NoSuchKeyException::forKeys($notFoundKeys);
171
        }
172
173 28
        foreach ($values as $key => $object) {
174 28
            $values[$key] = Serializer::unserialize($object->getData());
175
        }
176
177 27
        return $values;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183 12
    public function remove($key)
184
    {
185 12
        KeyUtil::validate($key);
186
187
        try {
188 10
            $object = $this->client->bucket($this->bucketName)->get($key);
189
190 9
            if (!$object->exists()) {
191 7
                return false;
192
            }
193
194 8
            $object->delete();
195 3
        } catch (Exception $e) {
196 3
            throw WriteException::forException($e);
197
        }
198
199 7
        return true;
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205 31
    public function exists($key)
206
    {
207 31
        KeyUtil::validate($key);
208
209
        try {
210 29
            return $this->client->bucket($this->bucketName)->get($key)->exists();
211 2
        } catch (Exception $e) {
212 2
            throw ReadException::forException($e);
213
        }
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219 90
    public function clear()
220
    {
221
        try {
222 90
            $bucket = $this->client->bucket($this->bucketName);
223
224 90
            foreach ($bucket->getKeys() as $key) {
225 90
                $bucket->get($key)->delete();
226
            }
227 2
        } catch (Exception $e) {
228 2
            throw WriteException::forException($e);
229
        }
230 90
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235 3
    public function keys()
236
    {
237
        try {
238 3
            return $this->client->bucket($this->bucketName)->getKeys();
239 1
        } catch (Exception $e) {
240 1
            throw ReadException::forException($e);
241
        }
242
    }
243
}
244