Completed
Push — v1.5.7 ( 10f56a )
by Bradley
02:37
created

Setting::get()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 2
Metric Value
c 6
b 0
f 2
dl 0
loc 26
rs 8.439
cc 5
eloc 15
nc 5
nop 2
1
<?php namespace Cornford\Setter;
2
3
use Cornford\Setter\Contracts\SettableInterface;
4
use Cornford\Setter\Exceptions\SettingVariableException;
5
6
class Setting extends SettingBase implements SettableInterface {
7
8
	/**
9
	 * Set a setting by key and value
10
	 *
11
	 * @param string  $key
12
	 * @param string  $value
13
	 *
14
	 * @return boolean
15
	 */
16
	public function set($key, $value)
17
	{
18
		$query = $this->database
19
			->table('settings');
20
		$value = json_encode($value);
21
22
		if ($this->has($key)) {
23
			$result = $query->where('key', $key)
24
				->update(array('value' => $value));
25
			$result = ($result == 0 ? true : $result);
26
		} else {
27
			$result = $query->insert(array('key' => $key, 'value' => $value));
28
		}
29
30
		$this->recacheItem($value, $key);
31
32
		return $result ? true : false;
33
	}
34
35
	/**
36
	 * Get a setting by key, optionally set a default or fallback to config lookup
37
	 *
38
	 * @param string  $key
39
	 * @param string  $default
40
	 *
41
	 * @return string|array|boolean
42
	 */
43
	public function get($key, $default = null)
44
	{
45
		if ($this->cacheHas($key)) {
46
			return $this->returnCache($key);
47
		}
48
49
		$results = $this->database
50
			->table('settings')
51
			->where('settings.key', '=', $key)
52
			->whereRaw('settings.key LIKE "' . $key . '.%"', array(), 'or')
53
			->lists('value', 'key');
54
55
		if ($results) {
56
			return $this->returnResults($results, $key);
57
		}
58
59
		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...
60
			return $default;
61
		}
62
63
		if ($this->configHas($key)) {
64
			return $this->returnConfig($key);
65
		}
66
67
		return false;
68
	}
69
70
	/**
71
	 * Forget a setting by key
72
	 *
73
	 * @param string $key
74
	 *
75
	 * @return boolean
76
	 */
77
	public function forget($key)
78
	{
79
		$result = $this->database
80
			->table('settings')
81
			->where('key', '=', $key)
82
			->delete();
83
84
		$this->cache
85
			->forget($this->attachTag($key));
86
87
		return $result ? true : false;
88
	}
89
90
	/**
91
	 * Check a setting exists by key
92
	 *
93
	 * @param string $key
94
	 *
95
	 * @return boolean
96
	 */
97
	public function has($key)
98
	{
99
		if ($this->cacheHas($this->attachTag($key))) {
100
			$result = true;
101
		} else {
102
			$result = $this->database
103
				->table('settings')
104
				->select('settings.value')
105
				->where('settings.key', '=', $key)
106
				->count();
107
		}
108
109
		return $result ? true : false;
110
	}
111
112
	/**
113
	 * Get all stored settings
114
	 *
115
	 * @return array
116
	 */
117
	public function all()
118
	{
119
		$results = $this->database
120
			->table('settings')
121
			->lists('value', 'key');
122
123
		return $this->arrangeResults($results);
124
	}
125
126
	/**
127
	 * Clear all stored settings
128
	 *
129
	 * @return boolean
130
	 */
131
	public function clear()
132
	{
133
		$result = $this->database
134
			->table('settings')
135
			->truncate();
136
137
		$this->cacheClear();
138
139
		return $result ? true : false;
140
	}
141
142
	/**
143
	 * Set the cache expiry
144
	 *
145
	 * @param boolean|integer|datetime $expiry
146
	 *
147
	 * @throws SettingVariableException
148
	 *
149
	 * @return self
150
	 */
151
	public function expires($expiry)
152
	{
153
		if (!is_bool($expiry) && !is_integer($expiry) && !$expiry instanceof \DateTime) {
154
			throw new SettingVariableException('Invalid expiry value.');
155
		}
156
157
		$this->expiry = $expiry;
158
159
		return $this;
160
	}
161
162
}
163