Completed
Push — master ( 15cddd...ae2a0d )
by Bradley
06:08 queued 03:34
created

Setting::enableCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php namespace Cornford\Setter;
2
3
use Cornford\Setter\Contracts\CacheableInterface;
4
use Cornford\Setter\Contracts\SettableInterface;
5
use Cornford\Setter\Exceptions\SettingArgumentException;
6
use DateTime;
7
8
class Setting extends SettingBase implements SettableInterface, CacheableInterface {
9
10
	/**
11
	 * Set a setting by key and value
12
	 *
13
	 * @param string  $key
14
	 * @param string  $value
15
	 *
16
	 * @return boolean
17
	 */
18
	public function set($key, $value)
19
	{
20
		$query = $this->database
0 ignored issues
show
Bug introduced by
The property database does not seem to exist. Did you mean databaseInstance?

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...
21
			->table('settings');
22
		$value = json_encode($value);
23
24
		if ($this->has($key)) {
25
			$result = $query->where('key', $key)
26
				->update(array('value' => $value));
27
		} else {
28
			$result = $query->insert(array('key' => $key, 'value' => $value));
29
		}
30
31
		if ($this->cacheEnabled()) {
32
			$this->recacheItem($value, $key);
33
		}
34
35
		return $result ? true : false;
36
	}
37
38
	/**
39
	 * Get a setting by key, optionally set a default or fallback to config lookup
40
	 *
41
	 * @param string  $key
42
	 * @param string  $default
43
	 *
44
	 * @return string|array|boolean
45
	 */
46
	public function get($key, $default = null)
47
	{
48
		if (!$this->getUncached() && $this->cacheEnabled() && $this->cacheHas($this->attachCacheTag($key))) {
49
			return $this->returnCache($key);
50
		}
51
52
		$results = $this->database
0 ignored issues
show
Bug introduced by
The property database does not seem to exist. Did you mean databaseInstance?

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...
53
			->table('settings')
54
			->where('settings.key', '=', $key)
55
			->whereRaw('settings.key LIKE "' . $key . '.%"', array(), 'or')
56
			->lists('value', 'key');
57
58
		$this->setUncached(false);
59
60
		if ($results) {
61
			return $this->returnResults($results, $key);
62
		}
63
		
64
		if ($default) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $default of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
65
			return $default;
66
		}
67
68
		if ($this->configHas($key)) {
69
			return $this->returnConfig($key);
70
		}
71
72
		return false;
73
	}
74
75
	/**
76
	 * Forget a setting by key
77
	 *
78
	 * @param string $key
79
	 *
80
	 * @return boolean
81
	 */
82
	public function forget($key)
83
	{
84
		$result = $this->database
0 ignored issues
show
Bug introduced by
The property database does not seem to exist. Did you mean databaseInstance?

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...
85
			->table('settings')
86
			->where('key', '=', $key)
87
			->delete();
88
89
		if ($this->cacheEnabled() && $this->cacheHas($this->attachCacheTag($key))) {
90
			$this->cache
91
				->forget($this->attachCacheTag($key));
92
		}
93
94
		return $result ? true : false;
95
	}
96
97
	/**
98
	 * Check a setting exists by key
99
	 *
100
	 * @param string $key
101
	 *
102
	 * @return boolean
103
	 */
104
	public function has($key)
105
	{
106
		if ($this->cacheEnabled() && $this->cacheHas($this->attachCacheTag($key))) {
107
			$result = true;
108
		} else {
109
			$result = $this->database
0 ignored issues
show
Bug introduced by
The property database does not seem to exist. Did you mean databaseInstance?

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...
110
				->table('settings')
111
				->select('settings.value')
112
				->where('settings.key', '=', $key)
113
				->count();
114
		}
115
116
		return ($result ? true : false);
117
	}
118
119
	/**
120
	 * Get all stored settings
121
	 *
122
	 * @return array
123
	 */
124
	public function all()
125
	{
126
		$results = $this->database
0 ignored issues
show
Bug introduced by
The property database does not seem to exist. Did you mean databaseInstance?

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
			->table('settings')
128
			->lists('value', 'key');
129
130
		return $this->arrangeResults($results);
131
	}
132
133
	/**
134
	 * Clear all stored settings
135
	 *
136
	 * @return boolean
137
	 */
138
	public function clear()
139
	{
140
		$result = $this->database
0 ignored issues
show
Bug introduced by
The property database does not seem to exist. Did you mean databaseInstance?

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...
141
			->table('settings')
142
			->truncate();
143
144
		if ($this->cacheEnabled()) {
145
			$this->cacheClear();			
146
		}
147
148
		return $result ? true : false;
149
	}
150
151
	/**
152
	 * Set the expiry
153
	 *
154
	 * @param boolean|integer|Datetime $expiry
155
	 *
156
	 * @throws SettingArgumentException
157
	 *
158
	 * @return self
159
	 */
160
	public function expires($expiry)
161
	{
162
		$this->cacheExpires($expiry);
163
164
		return $this;
165
	}
166
167
	/**
168
	 * Enable caching.
169
	 *
170
	 * @return self
171
	 */
172
	public function enableCache()
173
	{
174
		$this->setCacheEnabled(true);
175
176
		return $this;
177
	}
178
179
	/**
180
	 * Disable caching.
181
	 *
182
	 * @return self
183
	 */
184
	public function disableCache()
185
	{
186
		$this->setCacheEnabled(false);
187
188
		return $this;
189
	}
190
191
	/**
192
	 * Sets the uncached flag to request an item from the DB and re-cache the item.
193
	 *
194
	 * @return self
195
	 */
196
	public function uncached()
197
	{
198
		$this->setUncached(true);
199
200
		return $this;
201
	}
202
203
	/**
204
	 * Set the cache expiry
205
	 *
206
	 * @param boolean|integer|Datetime $expiry
207
	 *
208
	 * @throws SettingArgumentException
209
	 *
210
	 * @return self
211
	 */
212 View Code Duplication
	public function cacheExpires($expiry)
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...
213
	{
214
		if (!is_bool($expiry) && !is_integer($expiry) && !$expiry instanceof DateTime) {
215
			throw new SettingArgumentException('Expiry is required in boolean, integer or DateTime format.');
216
		}
217
218
		$this->cacheExpiry = $expiry;
219
220
		return $this;
221
	}
222
223
}
224