Code Duplication    Length = 195-196 lines in 2 locations

src/Cache/CouchbaseStore.php 1 location

@@ 25-220 (lines=196) @@
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
    public function __construct(CouchbaseCluster $cluster, $bucket, $password = '', $prefix = null, $serialize = 'php')
48
    {
49
        $this->cluster = $cluster;
50
        $this->setBucket($bucket, $password, $serialize);
51
        $this->setPrefix($prefix);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function get($key)
58
    {
59
        try {
60
            $result = $this->bucket->get($this->resolveKey($key));
61
62
            return $this->getMetaDoc($result);
63
        } catch (CouchbaseException $e) {
64
            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
    public function add($key, $value, $minutes = 0)
78
    {
79
        $options = ($minutes === 0) ? [] : ['expiry' => ($minutes * 60)];
80
        try {
81
            $this->bucket->insert($this->resolveKey($key), $value, $options);
82
83
            return true;
84
        } catch (CouchbaseException $e) {
85
            return false;
86
        }
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function put($key, $value, $minutes)
93
    {
94
        $this->bucket->upsert($this->resolveKey($key), $value, ['expiry' => $minutes * 60]);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function increment($key, $value = 1)
101
    {
102
        return $this->bucket
103
            ->counter($this->resolveKey($key), $value, ['initial' => abs($value)])->value;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function decrement($key, $value = 1)
110
    {
111
        return $this->bucket
112
            ->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
    public function forget($key)
127
    {
128
        $this->resolveKey($key);
129
        $this->bucket->remove($this->resolveKey($key));
130
    }
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
    public function getPrefix()
150
    {
151
        return $this->prefix;
152
    }
153
154
    /**
155
     * Set the cache key prefix.
156
     *
157
     * @param string $prefix
158
     */
159
    public function setPrefix($prefix)
160
    {
161
        $this->prefix = !empty($prefix) ? $prefix . ':' : '';
162
    }
163
164
    /**
165
     * @param        $bucket
166
     * @param string $password
167
     * @param string $serialize
168
     *
169
     * @return $this
170
     */
171
    public function setBucket($bucket, $password = '', $serialize = 'php')
172
    {
173
        $this->bucket = $this->cluster->openBucket($bucket, $password);
174
        if ($serialize === 'php') {
175
            $this->bucket->setTranscoder('couchbase_php_serialize_encoder', 'couchbase_default_decoder');
176
        }
177
178
        return $this;
179
    }
180
181
    /**
182
     * @param $keys
183
     *
184
     * @return array|string
185
     */
186
    private function resolveKey($keys)
187
    {
188
        if (is_array($keys)) {
189
            $result = [];
190
            foreach ($keys as $key) {
191
                $result[] = $this->prefix . $key;
192
            }
193
194
            return $result;
195
        }
196
197
        return $this->prefix . $keys;
198
    }
199
200
    /**
201
     * @param $meta
202
     *
203
     * @return array|null
204
     */
205
    protected function getMetaDoc($meta)
206
    {
207
        if ($meta instanceof \CouchbaseMetaDoc) {
208
            return $meta->value;
209
        }
210
        if (is_array($meta)) {
211
            $result = [];
212
            foreach ($meta as $row) {
213
                $result[] = $this->getMetaDoc($row);
214
            }
215
216
            return $result;
217
        }
218
219
        return;
220
    }
221
}
222

src/Cache/LegacyCouchbaseStore.php 1 location

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