Completed
Push — master ( 9b11b5...839864 )
by yuuki
10:11 queued 03:03
created

LegacyCouchbaseStore::getMetaDoc()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Importance

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