Completed
Push — master ( 0835cd...0d9cbc )
by yuuki
7s
created

CouchbaseStore::getMetaDoc()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 16
ccs 8
cts 9
cp 0.8889
crap 4.0218
rs 9.2
1
<?php
2
3
/**
4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10
 * THE SOFTWARE.
11
 */
12
13
namespace Ytake\LaravelCouchbase\Cache;
14
15
use CouchbaseBucket;
16
use CouchbaseCluster;
17
use CouchbaseException;
18
use Illuminate\Cache\TaggableStore;
19
use Illuminate\Contracts\Cache\Store;
20
use Illuminate\Cache\RetrievesMultipleKeys;
21
use Ytake\LaravelCouchbase\Exceptions\FlushException;
22
23
/**
24
 * Class CouchbaseStore.
25
 */
26
class CouchbaseStore extends TaggableStore implements Store
27
{
28
    use RetrievesMultipleKeys;
29
30
    /** @var string */
31
    protected $prefix;
32
33
    /** @var CouchbaseBucket */
34
    protected $bucket;
35
36
    /** @var CouchbaseCluster */
37
    protected $cluster;
38
39
    /**
40
     * CouchbaseStore constructor.
41
     *
42
     * @param CouchbaseCluster $cluster
43
     * @param                  $bucket
44
     * @param string           $password
45
     * @param null             $prefix
46
     * @param string           $serialize
47
     */
48 14
    public function __construct(CouchbaseCluster $cluster, $bucket, $password = '', $prefix = null, $serialize = 'php')
49
    {
50 14
        $this->cluster = $cluster;
51 14
        $this->setBucket($bucket, $password, $serialize);
52 14
        $this->setPrefix($prefix);
53 14
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 8
    public function get($key)
59
    {
60
        try {
61 8
            $result = $this->bucket->get($this->resolveKey($key));
62
63 6
            return $this->getMetaDoc($result);
64 3
        } catch (CouchbaseException $e) {
0 ignored issues
show
Bug introduced by
The class CouchbaseException does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
65 3
            return;
66
        }
67
    }
68
69
    /**
70
     * Store an item in the cache if the key doesn't exist.
71
     *
72
     * @param string|array $key
73
     * @param mixed        $value
74
     * @param int          $minutes
75
     *
76
     * @return bool
77
     */
78 7
    public function add($key, $value, $minutes = 0)
79
    {
80 7
        $options = ($minutes === 0) ? [] : ['expiry' => ($minutes * 60)];
81
        try {
82 7
            $this->bucket->insert($this->resolveKey($key), $value, $options);
83
84 7
            return true;
85 1
        } catch (CouchbaseException $e) {
0 ignored issues
show
Bug introduced by
The class CouchbaseException does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
86 1
            return false;
87
        }
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 1
    public function put($key, $value, $minutes)
94
    {
95 1
        $this->bucket->upsert($this->resolveKey($key), $value, ['expiry' => $minutes * 60]);
96 1
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 1
    public function increment($key, $value = 1)
102
    {
103 1
        return $this->bucket
104 1
            ->counter($this->resolveKey($key), $value, ['initial' => abs($value)])->value;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 1
    public function decrement($key, $value = 1)
111
    {
112 1
        return $this->bucket
113 1
            ->counter($this->resolveKey($key), (0 - abs($value)), ['initial' => (0 - abs($value))])->value;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 1
    public function forever($key, $value)
120
    {
121
        try {
122 1
            $this->bucket->insert($this->resolveKey($key), $value);
123 1
        } catch (CouchbaseException $e) {
0 ignored issues
show
Bug introduced by
The class CouchbaseException does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
124
            // bucket->insert when called from resetTag in TagSet can throw CAS exceptions, ignore.\
125 1
            $this->bucket->upsert($this->resolveKey($key), $value);
126
        }
127 1
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 8
    public function forget($key)
133
    {
134
        try {
135 8
            $this->bucket->remove($this->resolveKey($key));
136
        } catch (\Exception $e) {
137
            // Ignore exceptions from remove
138
        }
139 8
    }
140
141
    /**
142
     * flush bucket.
143
     *
144
     * @throws FlushException
145
     * @codeCoverageIgnore
146
     */
147
    public function flush()
148
    {
149
        $result = $this->bucket->manager()->flush();
150
        if (isset($result['_'])) {
151
            throw new FlushException($result);
152
        }
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158 1
    public function getPrefix()
159
    {
160 1
        return $this->prefix;
161
    }
162
163
    /**
164
     * Set the cache key prefix.
165
     *
166
     * @param string $prefix
167
     */
168 14
    public function setPrefix($prefix)
169
    {
170 14
        $this->prefix = !empty($prefix) ? $prefix.':' : '';
171 14
    }
172
173
    /**
174
     * @param        $bucket
175
     * @param string $password
176
     * @param string $serialize
177
     *
178
     * @return $this
179
     */
180 14
    public function setBucket($bucket, $password = '', $serialize = 'php')
181
    {
182 14
        $this->bucket = $this->cluster->openBucket($bucket, $password);
183 14
        if ($serialize === 'php') {
184 14
            $this->bucket->setTranscoder('couchbase_php_serialize_encoder', 'couchbase_default_decoder');
185
        }
186
187 14
        return $this;
188
    }
189
190
    /**
191
     * @param $keys
192
     *
193
     * @return array|string
194
     */
195 11
    private function resolveKey($keys)
196
    {
197 11
        if (is_array($keys)) {
198 1
            $result = [];
199 1
            foreach ($keys as $key) {
200 1
                $result[] = $this->prefix.$key;
201
            }
202
203 1
            return $result;
204
        }
205
206 10
        return $this->prefix.$keys;
207
    }
208
209
    /**
210
     * @param $meta
211
     *
212
     * @return array|null
213
     */
214 6
    protected function getMetaDoc($meta)
215
    {
216 6
        if ($meta instanceof \CouchbaseMetaDoc) {
0 ignored issues
show
Bug introduced by
The class CouchbaseMetaDoc does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
217 6
            return $meta->value;
218
        }
219 1
        if (is_array($meta)) {
220 1
            $result = [];
221 1
            foreach ($meta as $row) {
222 1
                $result[] = $this->getMetaDoc($row);
223
            }
224
225 1
            return $result;
226
        }
227
228
        return;
229
    }
230
}
231