Passed
Push — master ( 54f8d4...6f063c )
by Blizzz
13:59 queued 12s
created

Factory::__construct()   B

Complexity

Conditions 10
Paths 16

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 21
c 1
b 0
f 0
nc 16
nop 5
dl 0
loc 32
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Christoph Wurst <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Markus Goetz <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 * @author Robin Appelman <[email protected]>
11
 * @author Robin McCorkell <[email protected]>
12
 * @author Roeland Jago Douma <[email protected]>
13
 * @author Stefan Weil <[email protected]>
14
 * @author Vincent Petry <[email protected]>
15
 *
16
 * @license AGPL-3.0
17
 *
18
 * This code is free software: you can redistribute it and/or modify
19
 * it under the terms of the GNU Affero General Public License, version 3,
20
 * as published by the Free Software Foundation.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
 * GNU Affero General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Affero General Public License, version 3,
28
 * along with this program. If not, see <http://www.gnu.org/licenses/>
29
 *
30
 */
31
32
namespace OC\Memcache;
33
34
use OCP\ICache;
35
use OCP\ICacheFactory;
36
use OCP\ILogger;
37
use OCP\IMemcache;
38
39
class Factory implements ICacheFactory {
40
	public const NULL_CACHE = NullCache::class;
41
42
	/**
43
	 * @var string $globalPrefix
44
	 */
45
	private $globalPrefix;
46
47
	/**
48
	 * @var ILogger $logger
49
	 */
50
	private $logger;
51
52
	/**
53
	 * @var string $localCacheClass
54
	 */
55
	private $localCacheClass;
56
57
	/**
58
	 * @var string $distributedCacheClass
59
	 */
60
	private $distributedCacheClass;
61
62
	/**
63
	 * @var string $lockingCacheClass
64
	 */
65
	private $lockingCacheClass;
66
67
	/**
68
	 * @param string $globalPrefix
69
	 * @param ILogger $logger
70
	 * @param string|null $localCacheClass
71
	 * @param string|null $distributedCacheClass
72
	 * @param string|null $lockingCacheClass
73
	 */
74
	public function __construct(string $globalPrefix, ILogger $logger,
75
		$localCacheClass = null, $distributedCacheClass = null, $lockingCacheClass = null) {
76
		$this->logger = $logger;
77
		$this->globalPrefix = $globalPrefix;
78
79
		if (!$localCacheClass) {
80
			$localCacheClass = self::NULL_CACHE;
81
		}
82
		if (!$distributedCacheClass) {
83
			$distributedCacheClass = $localCacheClass;
84
		}
85
86
		$missingCacheMessage = 'Memcache {class} not available for {use} cache';
87
		$missingCacheHint = 'Is the matching PHP module installed and enabled?';
88
		if (!class_exists($localCacheClass) || !$localCacheClass::isAvailable()) {
89
			throw new \OC\HintException(strtr($missingCacheMessage, [
90
				'{class}' => $localCacheClass, '{use}' => 'local'
91
			]), $missingCacheHint);
92
		}
93
		if (!class_exists($distributedCacheClass) || !$distributedCacheClass::isAvailable()) {
94
			throw new \OC\HintException(strtr($missingCacheMessage, [
95
				'{class}' => $distributedCacheClass, '{use}' => 'distributed'
96
			]), $missingCacheHint);
97
		}
98
		if (!($lockingCacheClass && class_exists($distributedCacheClass) && $lockingCacheClass::isAvailable())) {
99
			// don't fallback since the fallback might not be suitable for storing lock
100
			$lockingCacheClass = self::NULL_CACHE;
101
		}
102
103
		$this->localCacheClass = $localCacheClass;
104
		$this->distributedCacheClass = $distributedCacheClass;
105
		$this->lockingCacheClass = $lockingCacheClass;
106
	}
107
108
	/**
109
	 * create a cache instance for storing locks
110
	 *
111
	 * @param string $prefix
112
	 * @return IMemcache
113
	 */
114
	public function createLocking(string $prefix = ''): IMemcache {
115
		return new $this->lockingCacheClass($this->globalPrefix . '/' . $prefix);
116
	}
117
118
	/**
119
	 * create a distributed cache instance
120
	 *
121
	 * @param string $prefix
122
	 * @return ICache
123
	 */
124
	public function createDistributed(string $prefix = ''): ICache {
125
		return new $this->distributedCacheClass($this->globalPrefix . '/' . $prefix);
126
	}
127
128
	/**
129
	 * create a local cache instance
130
	 *
131
	 * @param string $prefix
132
	 * @return ICache
133
	 */
134
	public function createLocal(string $prefix = ''): ICache {
135
		return new $this->localCacheClass($this->globalPrefix . '/' . $prefix);
136
	}
137
138
	/**
139
	 * check memcache availability
140
	 *
141
	 * @return bool
142
	 */
143
	public function isAvailable(): bool {
144
		return ($this->distributedCacheClass !== self::NULL_CACHE);
145
	}
146
147
	/**
148
	 * @see \OC\Memcache\Factory::createLocal()
149
	 * @param string $prefix
150
	 * @return ICache
151
	 */
152
	public function createLowLatency(string $prefix = ''): ICache {
153
		return $this->createLocal($prefix);
154
	}
155
156
	/**
157
	 * Check if a local memory cache backend is available
158
	 *
159
	 * @return bool
160
	 */
161
	public function isLocalCacheAvailable(): bool {
162
		return ($this->localCacheClass !== self::NULL_CACHE);
163
	}
164
}
165