Completed
Push — master ( 372cba...d14b76 )
by mw
70:46 queued 33:58
created

CacheFactory::newCacheByType()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SMW;
4
5
use ObjectCache;
6
use Onoi\Cache\CacheFactory as OnoiCacheFactory;
7
use RuntimeException;
8
9
/**
10
 * @license GNU GPL v2+
11
 * @since 2.2
12
 *
13
 * @author mwjames
14
 */
15
class CacheFactory {
16
17
	/**
18
	 * @var string|integer
19
	 */
20
	private $mainCacheType;
21
22
	/**
23
	 * @since 2.2
24
	 *
25
	 * @param string|integer|null $mainCacheType
26
	 */
27 348
	public function __construct( $mainCacheType = null ) {
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
28 348
		$this->mainCacheType = $mainCacheType;
29
30 348
		if ( $this->mainCacheType === null ) {
31 299
			$this->mainCacheType = $GLOBALS['smwgMainCacheType'];
32
		}
33 348
	}
34
35
	/**
36
	 * @since 2.2
37
	 *
38
	 * @return string|integer
39
	 */
40 303
	public function getMainCacheType() {
41 303
		return $this->mainCacheType;
42
	}
43
44
	/**
45
	 * @since 2.2
46
	 *
47
	 * @return string
48
	 */
49 307
	public function getCachePrefix() {
0 ignored issues
show
Coding Style introduced by
getCachePrefix uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
50 307
		return $GLOBALS['wgCachePrefix'] === false ? wfWikiID() : $GLOBALS['wgCachePrefix'];
51
	}
52
53
	/**
54
	 * @since 2.2
55
	 *
56
	 * @param string $key
57
	 *
58
	 * @return string
59
	 */
60 32
	public function getFactboxCacheKey( $key ) {
61 32
		return $this->getCachePrefix() . ':smw:fc:' . md5( $key );
62
	}
63
64
	/**
65
	 * @since 2.2
66
	 *
67
	 * @param string $key
68
	 *
69
	 * @return string
70
	 */
71 209
	public function getPurgeCacheKey( $key ) {
72 209
		return $this->getCachePrefix() . ':smw:arc:' . md5( $key );
73
	}
74
75
	/**
76
	 * @since 2.2
77
	 *
78
	 * @param array $cacheOptions
79
	 *
80
	 * @return stdClass
81
	 * @throws RuntimeException
82
	 */
83 191
	public function newCacheOptions( array $cacheOptions ) {
84
85 191
		if ( !isset( $cacheOptions['useCache'] ) || !isset( $cacheOptions['ttl'] ) ) {
86 1
			throw new RuntimeException( "Cache options is missing a useCache/ttl parameter" );
87
		}
88
89 190
		return (object)$cacheOptions;
90
	}
91
92
	/**
93
	 * @since 2.2
94
	 *
95
	 * @param integer $cacheSize
96
	 *
97
	 * @return Cache
98
	 */
99 342
	public function newFixedInMemoryCache( $cacheSize = 500 ) {
100 342
		return OnoiCacheFactory::getInstance()->newFixedInMemoryLruCache( $cacheSize );
101
	}
102
103
	/**
104
	 * @since 2.2
105
	 *
106
	 * @return Cache
107
	 */
108 1
	public function newNullCache() {
109 1
		return OnoiCacheFactory::getInstance()->newNullCache();
110
	}
111
112
	/**
113
	 * @since 2.2
114
	 *
115
	 * @param integer|string $mediaWikiCacheType
116
	 *
117
	 * @return Cache
118
	 */
119 306
	public function newMediaWikiCompositeCache( $mediaWikiCacheType = null ) {
120
121 306
		$compositeCache = OnoiCacheFactory::getInstance()->newCompositeCache( array(
122 306
			$this->newFixedInMemoryCache( 500 ),
123
			$this->newMediaWikiCache( $mediaWikiCacheType )
124
		) );
125 306
126 306
		return $compositeCache;
127 306
	}
128
129
	/**
130 306
	 * @since 2.5
131
	 *
132
	 * @param integer|string $mediaWikiCacheType
133
	 *
134
	 * @return Cache
135
	 */
136
	public function newMediaWikiCache( $mediaWikiCacheType = null ) {
137
138
		$mediaWikiCache = ObjectCache::getInstance(
139
			( $mediaWikiCacheType === null ? $this->getMainCacheType() : $mediaWikiCacheType )
140
		);
141
142 6
		return OnoiCacheFactory::getInstance()->newMediaWikiCache( $mediaWikiCache );
143 6
	}
144
145
	/**
146
	 * @since 2.5
147
	 *
148
	 * @param integer|null $cacheType
149
	 *
150
	 * @return Cache
151
	 */
152
	public function newCacheByType( $cacheType = null ) {
153
154
		if ( $cacheType === CACHE_NONE || $cacheType === null ) {
155
			return $this->newNullCache();
156
		}
157
158
		return $this->newMediaWikiCache( $cacheType );
159
	}
160
161
	/**
162
	 * @since 2.4
163
	 *
164
	 * @param string $namespace
165
	 * @param string|integer|null $cacheType
166
	 * @param integer $cacheLifetime
167
	 *
168
	 * @return BlobStore
169
	 */
170
	public function newBlobStore( $namespace, $cacheType = null, $cacheLifetime = 0 ) {
171
		return ApplicationFactory::getInstance()->create( 'BlobStore', $namespace, $cacheType, $cacheLifetime );
172
	}
173
174
}
175