Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

classes/Elgg/Database/PrivateSettingsTable.php (2 issues)

1
<?php
2
3
namespace Elgg\Database;
4
5
use DatabaseException;
6
use Elgg\Database;
7
use ElggCache;
8
9
/**
10
 * Private settings for entities
11
 *
12
 * Private settings provide metadata like storage of settings for plugins
13
 * and users.
14
 *
15
 * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
16
 *
17
 * @access private
18
 * @since 2.0.0
19
 */
20
class PrivateSettingsTable {
21
22
	/**
23
	 * @var Database
24
	 */
25
	protected $db;
26
27
	/**
28
	 * @var EntityTable
29
	 */
30
	protected $entities;
31
32
	/**
33
	 * @var string
34
	 */
35
	protected $table;
36
37
	/**
38
	 * @var ElggCache
39
	 */
40
	protected $cache;
41
42
	/**
43
	 * Constructor
44
	 *
45
	 * @param Database    $db       The database
46
	 * @param EntityTable $entities Entities table
47
	 * @param ElggCache   $cache    Settings cache
48
	 */
49 27
	public function __construct(Database $db, EntityTable $entities, ElggCache $cache) {
50 27
		$this->db = $db;
51 27
		$this->entities = $entities;
52 27
		$this->cache = $cache;
53 27
		$this->table = $this->db->prefix . 'private_settings';
54 27
	}
55
56
	/**
57
	 * Gets a private setting for an entity
58
	 *
59
	 * Plugin authors can set private data on entities. By default private
60
	 * data will not be searched or exported.
61
	 *
62
	 * @param int    $entity_guid The entity GUID
63
	 * @param string $name        The name of the setting
64
	 *
65
	 * @return mixed The setting value, or null if does not exist
66
	 * @throws DatabaseException
67
	 */
68 38
	public function get($entity_guid, $name) {
69 38
		$values = $this->cache->load($entity_guid);
70
71 38
		if (isset($values[$name])) {
72
			return $values[$name];
73
		}
74
75 38
		if (!$this->entities->exists($entity_guid)) {
76
			return false;
77
		}
78
79 38
		$qb = Select::fromTable('private_settings');
80 38
		$qb->select('name')
81 38
			->addSelect('value')
82 38
			->where($qb->compare('name', '=', $name, ELGG_VALUE_STRING))
83 38
			->andWhere($qb->compare('entity_guid', '=', $entity_guid, ELGG_VALUE_INTEGER));
84
85 38
		$setting = $this->db->getDataRow($qb);
86 38
		if ($setting) {
87 18
			return $setting->value;
88
		}
89
90 30
		return null;
91
	}
92
93
	/**
94
	 * Return an array of all private settings.
95
	 *
96
	 * @param int $entity_guid The entity GUID
97
	 *
98
	 * @return string[] empty array if no settings
99
	 * @throws DatabaseException
100
	 */
101
	public function getAll($entity_guid) {
102
		if (!$this->entities->exists($entity_guid)) {
103
			return [];
104
		}
105
106
		$values = $this->cache->load($entity_guid);
107
		if (isset($values)) {
108
			return $values;
109
		}
110
111
		$qb = Select::fromTable('private_settings');
112
		$qb->select('name')
113
			->addSelect('value')
114
			->where($qb->compare('entity_guid', '=', $entity_guid, ELGG_VALUE_INTEGER));
115
116
		$result = $this->db->getData($qb);
117
118
		$return = [];
119
120
		if ($result) {
121
			foreach ($result as $r) {
122
				$return[$r->name] = $r->value;
123
			}
124
		}
125
126
		$this->cache->save($entity_guid, $return);
127
128
		return $return;
129
	}
130
131
	/**
132
	 * Sets a private setting for an entity.
133
	 *
134
	 * @param int    $entity_guid The entity GUID
135
	 * @param string $name        The name of the setting
136
	 * @param string $value       The value of the setting
137
	 *
138
	 * @return bool
139
	 * @throws DatabaseException
140
	 */
141 124
	public function set($entity_guid, $name, $value) {
142 124
		if (!$this->entities->exists($entity_guid)) {
143
			return false;
144
		}
145
146 124
		$this->entities->invalidateCache($entity_guid);
147
148 124
		$qb = Select::fromTable('private_settings');
149 124
		$qb->select('id')
150 124
			->where($qb->compare('name', '=', $name, ELGG_VALUE_STRING))
151 124
			->andWhere($qb->compare('entity_guid', '=', $entity_guid, ELGG_VALUE_INTEGER));
152
153 124
		$row = $this->db->getDataRow($qb);
154
155 124
		if ($row) {
156 1
			$qb = Update::table('private_settings');
157 1
			$qb->set('value', $qb->param($value, ELGG_VALUE_STRING))
158 1
				->where($qb->compare('id', '=', $row->id, ELGG_VALUE_INTEGER));
159
160 1
			$result = $this->db->updateData($qb);
161
		} else {
162 124
			$qb = Insert::intoTable('private_settings');
163 124
			$qb->values([
164 124
				'entity_guid' => $qb->param($entity_guid, ELGG_VALUE_INTEGER),
165 124
				'name' => $qb->param($name, ELGG_VALUE_STRING),
166 124
				'value' => $qb->param($value, ELGG_VALUE_STRING),
167
			]);
168
169 124
			$result = $this->db->insertData($qb);
170
		}
171
172 124
		return $result !== false;
173
	}
174
175
	/**
176
	 * Deletes a private setting for an entity.
177
	 *
178
	 * @param int    $entity_guid The Entity GUID
179
	 * @param string $name        The name of the setting
180
	 *
181
	 * @return bool
182
	 * @throws DatabaseException
183
	 */
184 3
	public function remove($entity_guid, $name) {
185 3
		$this->entities->invalidateCache($entity_guid);
186
187 3
		$qb = Delete::fromTable('private_settings');
188 3
		$qb->where($qb->compare('name', '=', $name, ELGG_VALUE_STRING))
189 3
			->andWhere($qb->compare('entity_guid', '=', $entity_guid, ELGG_VALUE_INTEGER));
190
191 3
		return $this->db->deleteData($qb);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->db->deleteData($qb) returns the type integer which is incompatible with the documented return type boolean.
Loading history...
192
	}
193
194
	/**
195
	 * Deletes all private settings for an entity
196
	 *
197
	 * @param int $entity_guid The Entity GUID
198
	 *
199
	 * @return bool
200
	 * @throws DatabaseException
201
	 */
202 213
	public function removeAllForEntity($entity_guid) {
203 213
		$this->entities->invalidateCache($entity_guid);
204
205 213
		$qb = Delete::fromTable('private_settings');
206 213
		$qb->where($qb->compare('entity_guid', '=', $entity_guid, ELGG_VALUE_INTEGER));
207
208 213
		return $this->db->deleteData($qb);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->db->deleteData($qb) returns the type integer which is incompatible with the documented return type boolean.
Loading history...
209
	}
210
211
}
212