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

XcacheCache   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 77
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A doFlush() 0 7 1
A doSave() 0 3 1
A checkAuthorization() 0 6 2
A doDelete() 0 3 1
A doContains() 0 3 1
A doGetStats() 0 11 1
A doFetch() 0 3 2
1
<?php
2
3
namespace Doctrine\Common\Cache;
4
5
use const XC_TYPE_VAR;
0 ignored issues
show
Bug introduced by
The constant XC_TYPE_VAR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
6
use function ini_get;
7
use function serialize;
8
use function unserialize;
9
use function xcache_clear_cache;
10
use function xcache_get;
11
use function xcache_info;
12
use function xcache_isset;
13
use function xcache_set;
14
use function xcache_unset;
15
16
/**
17
 * Xcache cache driver.
18
 *
19
 * @link   www.doctrine-project.org
20
 */
21
class XcacheCache extends CacheProvider
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected function doFetch($id)
27
    {
28
        return $this->doContains($id) ? unserialize(xcache_get($id)) : false;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected function doContains($id)
35
    {
36
        return xcache_isset($id);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function doSave($id, $data, $lifeTime = 0)
43
    {
44
        return xcache_set($id, serialize($data), (int) $lifeTime);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function doDelete($id)
51
    {
52
        return xcache_unset($id);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function doFlush()
59
    {
60
        $this->checkAuthorization();
61
62
        xcache_clear_cache(XC_TYPE_VAR);
0 ignored issues
show
Bug introduced by
The constant XC_TYPE_VAR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
63
64
        return true;
65
    }
66
67
    /**
68
     * Checks that xcache.admin.enable_auth is Off.
69
     *
70
     * @return void
71
     *
72
     * @throws \BadMethodCallException When xcache.admin.enable_auth is On.
73
     */
74
    protected function checkAuthorization()
75
    {
76
        if (ini_get('xcache.admin.enable_auth')) {
77
            throw new \BadMethodCallException(
78
                'To use all features of \Doctrine\Common\Cache\XcacheCache, '
79
                . 'you must set "xcache.admin.enable_auth" to "Off" in your php.ini.'
80
            );
81
        }
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    protected function doGetStats()
88
    {
89
        $this->checkAuthorization();
90
91
        $info = xcache_info(XC_TYPE_VAR, 0);
0 ignored issues
show
Bug introduced by
The constant XC_TYPE_VAR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
92
        return [
93
            Cache::STATS_HITS   => $info['hits'],
94
            Cache::STATS_MISSES => $info['misses'],
95
            Cache::STATS_UPTIME => null,
96
            Cache::STATS_MEMORY_USAGE      => $info['size'],
97
            Cache::STATS_MEMORY_AVAILABLE  => $info['avail'],
98
        ];
99
    }
100
}
101