Completed
Branch release-2.1 (e49a83)
by Mert
04:10
created

memcached_cache::cacheSettings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Simple Machines Forum (SMF)
5
 *
6
 * @package SMF
7
 * @author Simple Machines http://www.simplemachines.org
8
 * @copyright 2016 Simple Machines and individual contributors
9
 * @license http://www.simplemachines.org/about/smf/license.php BSD
10
 *
11
 * @version 2.1 Beta 3
12
 */
13
14
if (!defined('SMF'))
15
	die('Hacking attempt...');
16
17
/**
18
 * Our Cache API class
19
 * @package cacheAPI
20
 */
21
class memcached_cache extends cache_api
22
{
23
	/**
24
	 * @var string The memcache instance.
25
	 */
26
	private $memcache = null;
27
28
	/**
29
	 * {@inheritDoc}
30
	 */
31 View Code Duplication
	public function isSupported($test = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
	{
33
		global $cache_memcached;
34
35
		$supported = class_exists('memcached');
36
37
		if ($test)
38
			return $supported;
39
		return parent::isSupported() && $supported && !empty($cache_memcached);
40
	}
41
42
	/**
43
	 * {@inheritDoc}
44
	 */
45
	public function connect()
46
	{
47
		global $db_persist, $cache_memcached;
48
49
		$servers = explode(',', $cache_memcached);
50
		$port = 0;
0 ignored issues
show
Unused Code introduced by
$port is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
52
		// memcached does not remove servers from the list upon completing the script under modes like FastCGI. So check to see if servers exist or not.
53
		$this->memcached = new Memcached;
0 ignored issues
show
Bug introduced by
The property memcached does not seem to exist. Did you mean memcache?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
54
		$currentServers = $this->memcached->getServerList();
0 ignored issues
show
Bug introduced by
The property memcached does not seem to exist. Did you mean memcache?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
55
		foreach ($servers as $server)
56
		{
57
			if (strpos($server,'/') !== false)
58
				$tempServer = array($server, 0);
59 View Code Duplication
			else
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
			{
61
				$server = explode(':', $server);
62
				$tempServer = array($server[0], isset($server[1]) ? $server[1] : 11211);
63
			}
64
65
			// Figure out if we have this server or not
66
			$foundServer = false;
67
			foreach ($currentServers as $currentServer)
68
			{
69
				if ($tempServer[0] == $currentServer['host'] && $tempServer[1] == $currentServer['port'])
70
				{
71
					$foundServer = true;
72
					break;
73
				}
74
			}
75
76
			// Found it?
77
			if (empty($foundServer))
78
				$this->memcached->addServer($tempServer[0], $tempServer[1]);
0 ignored issues
show
Bug introduced by
The property memcached does not seem to exist. Did you mean memcache?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
79
		}
80
81
		// Best guess is this worked.
82
		return true;
83
	}
84
85
	/**
86
	 * {@inheritDoc}
87
	 */
88 View Code Duplication
	public function getData($key, $ttl = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
	{
90
		$key = $this->prefix . strtr($key, ':/', '-_');
91
92
		$value = $this->memcached->get($key);
0 ignored issues
show
Bug introduced by
The property memcached does not seem to exist. Did you mean memcache?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
93
94
		// $value should return either data or false (from failure, key not found or empty array).
95
		if ($value === false)
96
			return null;
97
		return $value;
98
	}
99
100
	/**
101
	 * {@inheritDoc}
102
	 */
103
	public function putData($key, $value, $ttl = null)
104
	{
105
		$key = $this->prefix . strtr($key, ':/', '-_');
106
107
		return $this->memcached->set($key, $value, $ttl);
0 ignored issues
show
Bug introduced by
The property memcached does not seem to exist. Did you mean memcache?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
108
	}
109
110
	/**
111
	 * {@inheritDoc}
112
	 */
113
	public function cleanCache($type = '')
114
	{
115
		$this->invalidateCache();
116
117
		// Memcached accepts a delay parameter, always use 0 (instant).
118
		return $this->memcached->flush(0);
0 ignored issues
show
Bug introduced by
The property memcached does not seem to exist. Did you mean memcache?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
119
	}
120
121
	/**
122
	 * {@inheritDoc}
123
	 */
124
	public function quit()
125
	{
126
		return $this->memcached->quit();
0 ignored issues
show
Bug introduced by
The property memcached does not seem to exist. Did you mean memcache?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
127
	}
128
129
	/**
130
	 * {@inheritDoc}
131
	 */
132 View Code Duplication
	public function cacheSettings(array &$config_vars)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
	{
134
		global $context, $txt;
135
136
		$config_vars[] = $txt['cache_memcache_settings'];
137
		$config_vars[] = array('cache_memcached', $txt['cache_memcache_servers'], 'file', 'text', 0, 'cache_memcached', 'postinput' => '<br /><div class="smalltext"><em>' . $txt['cache_memcache_servers_subtext'] . '</em></div>');
138
139
		if (!isset($context['settings_post_javascript']))
140
			$context['settings_post_javascript'] = '';
141
142
		$context['settings_post_javascript'] .= '
143
			$("#cache_accelerator").change(function (e) {
144
				var cache_type = e.currentTarget.value;
145
				$("#cache_memcached").prop("disabled", cache_type != "memcached");
146
			});';
147
	}
148
}