Completed
Pull Request — master (#24)
by
unknown
07:48
created

RiakStore::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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