Passed
Push — master ( b2988e...9f380c )
by Jonathan
33:05
created

CouchbaseCache::doFetch()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Common\Cache;
4
5
use Couchbase;
6
use function explode;
7
use function time;
8
9
/**
10
 * Couchbase cache provider.
11
 *
12
 * @link   www.doctrine-project.org
13
 * @deprecated Couchbase SDK 1.x is now deprecated. Use \Doctrine\Common\Cache\CouchbaseBucketCache instead.
14
 * https://developer.couchbase.com/documentation/server/current/sdk/php/compatibility-versions-features.html
15
 */
16
class CouchbaseCache extends CacheProvider
17
{
18
    /** @var Couchbase|null */
19
    private $couchbase;
20
21
    /**
22
     * Sets the Couchbase instance to use.
23
     *
24
     * @return void
25
     */
26
    public function setCouchbase(Couchbase $couchbase)
27
    {
28
        $this->couchbase = $couchbase;
29
    }
30
31
    /**
32
     * Gets the Couchbase instance used by the cache.
33
     *
34
     * @return Couchbase|null
35
     */
36
    public function getCouchbase()
37
    {
38
        return $this->couchbase;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected function doFetch($id)
45
    {
46
        return $this->couchbase->get($id) ?: false;
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        return $this->couchbase->/** @scrutinizer ignore-call */ get($id) ?: false;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function doContains($id)
53
    {
54
        return $this->couchbase->get($id) !== null;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    protected function doSave($id, $data, $lifeTime = 0)
61
    {
62
        if ($lifeTime > 30 * 24 * 3600) {
63
            $lifeTime = time() + $lifeTime;
64
        }
65
        return $this->couchbase->set($id, $data, (int) $lifeTime);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    protected function doDelete($id)
72
    {
73
        return $this->couchbase->delete($id);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    protected function doFlush()
80
    {
81
        return $this->couchbase->flush();
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    protected function doGetStats()
88
    {
89
        $stats   = $this->couchbase->getStats();
90
        $servers = $this->couchbase->getServers();
91
        $server  = explode(':', $servers[0]);
92
        $key     = $server[0] . ':11210';
93
        $stats   = $stats[$key];
94
        return [
95
            Cache::STATS_HITS   => $stats['get_hits'],
96
            Cache::STATS_MISSES => $stats['get_misses'],
97
            Cache::STATS_UPTIME => $stats['uptime'],
98
            Cache::STATS_MEMORY_USAGE     => $stats['bytes'],
99
            Cache::STATS_MEMORY_AVAILABLE => $stats['limit_maxbytes'],
100
        ];
101
    }
102
}
103