Completed
Push — master ( ae2a0d...b829ba )
by Bradley
03:35 queued 01:41
created

Setting::uncached()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
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
			$result = ($result == 0 ? true : $result);
28
		} else {
29
			$result = $query->insert(array('key' => $key, 'value' => $value));
30
		}
31
32
		if ($this->cacheEnabled()) {
33
			$this->recacheItem($value, $key);
34
		}
35
36
		return $result ? true : false;
37
	}
38
39
	/**
40
	 * Get a setting by key, optionally set a default or fallback to config lookup
41
	 *
42
	 * @param string  $key
43
	 * @param string  $default
44
	 *
45
	 * @return string|array|boolean
46
	 */
47
	public function get($key, $default = null)
48
	{
49
		if (!$this->getUncached() && $this->cacheEnabled() && $this->cacheHas($this->attachCacheTag($key))) {
50
			return $this->returnCache($key);
51
		}
52
53
		$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...
54
			->table('settings')
55
			->where('settings.key', '=', $key)
56
			->whereRaw('settings.key LIKE "' . $key . '.%"', array(), 'or')
57
			->lists('value', 'key');
58
59
		$this->setUncached(false);
60
61
		if ($results) {
62
			return $this->returnResults($results, $key);
63
		}
64
		
65
		if ($default !== null) {
66
			return $default;
67
		}
68
69
		if ($this->configHas($key)) {
70
			return $this->returnConfig($key);
71
		}
72
73
		return false;
74
	}
75
76
	/**
77
	 * Forget a setting by key
78
	 *
79
	 * @param string $key
80
	 *
81
	 * @return boolean
82
	 */
83
	public function forget($key)
84
	{
85
		$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...
86
			->table('settings')
87
			->where('key', '=', $key)
88
			->delete();
89
90
		if ($this->cacheEnabled() && $this->cacheHas($this->attachCacheTag($key))) {
91
			$this->cache
92
				->forget($this->attachCacheTag($key));
93
		}
94
95
		return $result ? true : false;
96
	}
97
98
	/**
99
	 * Check a setting exists by key
100
	 *
101
	 * @param string $key
102
	 *
103
	 * @return boolean
104
	 */
105
	public function has($key)
106
	{
107
		if ($this->cacheEnabled() && $this->cacheHas($this->attachCacheTag($key))) {
108
			$result = true;
109
		} else {
110
			$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...
111
				->table('settings')
112
				->select('settings.value')
113
				->where('settings.key', '=', $key)
114
				->count();
115
		}
116
117
		return ($result ? true : false);
118
	}
119
120
	/**
121
	 * Get all stored settings
122
	 *
123
	 * @return array
124
	 */
125
	public function all()
126
	{
127
		$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...
128
			->table('settings')
129
			->lists('value', 'key');
130
131
		return $this->arrangeResults($results);
132
	}
133
134
	/**
135
	 * Clear all stored settings
136
	 *
137
	 * @return boolean
138
	 */
139
	public function clear()
140
	{
141
		$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...
142
			->table('settings')
143
			->truncate();
144
145
		if ($this->cacheEnabled()) {
146
			$this->cacheClear();			
147
		}
148
149
		return $result ? true : false;
150
	}
151
152
	/**
153
	 * Set the expiry
154
	 *
155
	 * @param boolean|integer|Datetime $expiry
156
	 *
157
	 * @throws SettingArgumentException
158
	 *
159
	 * @return self
160
	 */
161
	public function expires($expiry)
162
	{
163
		$this->cacheExpires($expiry);
164
165
		return $this;
166
	}
167
168
	/**
169
	 * Enable caching.
170
	 *
171
	 * @return self
172
	 */
173
	public function enableCache()
174
	{
175
		$this->setCacheEnabled(true);
176
177
		return $this;
178
	}
179
180
	/**
181
	 * Disable caching.
182
	 *
183
	 * @return self
184
	 */
185
	public function disableCache()
186
	{
187
		$this->setCacheEnabled(false);
188
189
		return $this;
190
	}
191
192
	/**
193
	 * Sets the uncached flag to request an item from the DB and re-cache the item.
194
	 *
195
	 * @return self
196
	 */
197
	public function uncached()
198
	{
199
		$this->setUncached(true);
200
201
		return $this;
202
	}
203
204
	/**
205
	 * Set the cache expiry
206
	 *
207
	 * @param boolean|integer|Datetime $expiry
208
	 *
209
	 * @throws SettingArgumentException
210
	 *
211
	 * @return self
212
	 */
213 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...
214
	{
215
		if (!is_bool($expiry) && !is_integer($expiry) && !$expiry instanceof DateTime) {
216
			throw new SettingArgumentException('Expiry is required in boolean, integer or DateTime format.');
217
		}
218
219
		$this->cacheExpiry = $expiry;
220
221
		return $this;
222
	}
223
224
}
225