Completed
Push — master ( 839864...5510b6 )
by yuuki
07:07
created

CouchbaseStore::setPrefix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 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
namespace Ytake\LaravelCouchbase\Cache;
13
14
use CouchbaseBucket;
15
use CouchbaseCluster;
16
use CouchbaseException;
17
use Illuminate\Cache\TaggableStore;
18
use Illuminate\Contracts\Cache\Store;
19
use Illuminate\Cache\RetrievesMultipleKeys;
20
use Ytake\LaravelCouchbase\Exceptions\FlushException;
21
22
/**
23
 * Class CouchbaseStore.
24
 */
25
class CouchbaseStore extends TaggableStore implements Store
26
{
27
    use RetrievesMultipleKeys;
28
29
    /** @var string */
30
    protected $prefix;
31
32
    /** @var CouchbaseBucket */
33
    protected $bucket;
34
35
    /** @var CouchbaseCluster */
36
    protected $cluster;
37
38
    /**
39
     * CouchbaseStore constructor.
40
     *
41
     * @param CouchbaseCluster $cluster
42
     * @param                  $bucket
43
     * @param string           $password
44
     * @param null             $prefix
45
     * @param string           $serialize
46
     */
47 13
    public function __construct(CouchbaseCluster $cluster, $bucket, $password = '', $prefix = null, $serialize = 'php')
48
    {
49 13
        $this->cluster = $cluster;
50 13
        $this->setBucket($bucket, $password, $serialize);
51 13
        $this->setPrefix($prefix);
52 13
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 6
    public function get($key)
58
    {
59
        try {
60 6
            $result = $this->bucket->get($this->resolveKey($key));
61
62 5
            return $this->getMetaDoc($result);
63 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...
64 1
            return;
65
        }
66
    }
67
68
    /**
69
     * Store an item in the cache if the key doesn't exist.
70
     *
71
     * @param string|array $key
72
     * @param mixed        $value
73
     * @param int          $minutes
74
     *
75
     * @return bool
76
     */
77 5
    public function add($key, $value, $minutes = 0)
78
    {
79 5
        $options = ($minutes === 0) ? [] : ['expiry' => ($minutes * 60)];
80
        try {
81 5
            $this->bucket->insert($this->resolveKey($key), $value, $options);
82
83 5
            return true;
84 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...
85 1
            return false;
86
        }
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 1
    public function put($key, $value, $minutes)
93
    {
94 1
        $this->bucket->upsert($this->resolveKey($key), $value, ['expiry' => $minutes * 60]);
95 1
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 1
    public function increment($key, $value = 1)
101
    {
102 1
        return $this->bucket
103 1
            ->counter($this->resolveKey($key), $value, ['initial' => abs($value)])->value;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 1
    public function decrement($key, $value = 1)
110
    {
111 1
        return $this->bucket
112 1
            ->counter($this->resolveKey($key), (0 - abs($value)), ['initial' => (0 - abs($value))])->value;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function forever($key, $value)
119
    {
120
        $this->bucket->insert($this->resolveKey($key), $value);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 8
    public function forget($key)
127
    {
128 8
        $this->resolveKey($key);
129 8
        $this->bucket->remove($this->resolveKey($key));
130 8
    }
131
132
    /**
133
     * flush bucket.
134
     *
135
     * @throws FlushException
136
     * @codeCoverageIgnore
137
     */
138
    public function flush()
139
    {
140
        $result = $this->bucket->manager()->flush();
141
        if (isset($result['_'])) {
142
            throw new FlushException($result);
143
        }
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149 1
    public function getPrefix()
150
    {
151 1
        return $this->prefix;
152
    }
153
154
    /**
155
     * Set the cache key prefix.
156
     *
157
     * @param string $prefix
158
     */
159 13
    public function setPrefix($prefix)
160
    {
161 13
        $this->prefix = !empty($prefix) ? $prefix . ':' : '';
162 13
    }
163
164
    /**
165
     * @param        $bucket
166
     * @param string $password
167
     * @param string $serialize
168
     *
169
     * @return $this
170
     */
171 13
    public function setBucket($bucket, $password = '', $serialize = 'php')
172
    {
173 13
        $this->bucket = $this->cluster->openBucket($bucket, $password);
174 13
        if($serialize === 'php') {
175 13
            $this->bucket->setTranscoder('couchbase_php_serialize_encoder', 'couchbase_default_decoder');
176 13
        }
177 13
        return $this;
178
    }
179
180
    /**
181
     * @param $keys
182
     *
183
     * @return array|string
184
     */
185 9
    private function resolveKey($keys)
186
    {
187 9
        if (is_array($keys)) {
188 1
            $result = [];
189 1
            foreach ($keys as $key) {
190 1
                $result[] = $this->prefix . $key;
191 1
            }
192
193 1
            return $result;
194
        }
195
196 8
        return $this->prefix . $keys;
197
    }
198
199
    /**
200
     * @param $meta
201
     *
202
     * @return array|null
203
     */
204 5
    protected function getMetaDoc($meta)
205
    {
206 5
        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...
207 5
            return $meta->value;
208
        }
209 1
        if (is_array($meta)) {
210 1
            $result = [];
211 1
            foreach ($meta as $row) {
212 1
                $result[] = $this->getMetaDoc($row);
213 1
            }
214
215 1
            return $result;
216
        }
217
218
        return;
219
    }
220
}
221