Completed
Push — develop ( e185ca...3573a2 )
by Bradley
01:56
created

Setting::cacheEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
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->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
		if ($results) {
59
			return $this->returnResults($results, $key);
60
		}
61
62
		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...
63
			return $default;
64
		}
65
66
		if ($this->configHas($key)) {
67
			return $this->returnConfig($key);
68
		}
69
70
		return false;
71
	}
72
73
	/**
74
	 * Forget a setting by key
75
	 *
76
	 * @param string $key
77
	 *
78
	 * @return boolean
79
	 */
80
	public function forget($key)
81
	{
82
		$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...
83
			->table('settings')
84
			->where('key', '=', $key)
85
			->delete();
86
87
		if ($this->cacheEnabled() && $this->cacheHas($this->attachCacheTag($key))) {
88
			$this->cache
89
				->forget($this->attachCacheTag($key));
90
		}
91
92
		return $result ? true : false;
93
	}
94
95
	/**
96
	 * Check a setting exists by key
97
	 *
98
	 * @param string $key
99
	 *
100
	 * @return boolean
101
	 */
102
	public function has($key)
103
	{
104
		if ($this->cacheEnabled() && $this->cacheHas($this->attachCacheTag($key))) {
105
			$result = true;
106
		} else {
107
			$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...
108
				->table('settings')
109
				->select('settings.value')
110
				->where('settings.key', '=', $key)
111
				->count();
112
		}
113
114
		return ($result ? true : false);
115
	}
116
117
	/**
118
	 * Get all stored settings
119
	 *
120
	 * @return array
121
	 */
122
	public function all()
123
	{
124
		$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...
125
			->table('settings')
126
			->lists('value', 'key');
127
128
		return $this->arrangeResults($results);
129
	}
130
131
	/**
132
	 * Clear all stored settings
133
	 *
134
	 * @return boolean
135
	 */
136
	public function clear()
137
	{
138
		$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...
139
			->table('settings')
140
			->truncate();
141
142
		if ($this->cacheEnabled()) {
143
			$this->cacheClear();			
144
		}
145
146
		return $result ? true : false;
147
	}
148
149
	/**
150
	 * Set the expiry
151
	 *
152
	 * @param boolean|integer|Datetime $expiry
153
	 *
154
	 * @throws SettingArgumentException
155
	 *
156
	 * @return self
157
	 */
158
	public function expires($expiry)
159
	{
160
		$this->cacheExpires($expiry);
161
162
		return $this;
163
	}
164
165
	/**
166
	 * Enable caching.
167
	 *
168
	 * @return self
169
	 */
170
	public function enableCache()
171
	{
172
		$this->setCacheEnabled(true);
173
174
		return $this;
175
	}
176
177
	/**
178
	 * Disable caching.
179
	 *
180
	 * @return self
181
	 */
182
	public function disableCache()
183
	{
184
		$this->setCacheEnabled(false);
185
186
		return $this;
187
	}
188
189
	/**
190
	 * Set the cache expiry
191
	 *
192
	 * @param boolean|integer|Datetime $expiry
193
	 *
194
	 * @throws SettingArgumentException
195
	 *
196
	 * @return self
197
	 */
198 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...
199
	{
200
		if (!is_bool($expiry) && !is_integer($expiry) && !$expiry instanceof DateTime) {
201
			throw new SettingArgumentException('Expiry is required in boolean, integer or DateTime format.');
202
		}
203
204
		$this->cacheExpiry = $expiry;
205
206
		return $this;
207
	}
208
209
}
210