Db::setUserRole()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Elgg\Roles;
4
5
class Db implements \Elgg\Roles\DbInterface {
6
7
	/**
8
	 * {@inheritdoc}
9
	 */
10
	public function getAllRoles() {
11
		$options = array(
12
			'type' => 'object',
13
			'subtype' => 'role',
14
			'limit' => 0
15
		);
16
		return new \ElggBatch('elgg_get_entities', $options);
17
	}
18
19
	/**
20
	 * {@inheritdoc}
21
	 */
22
	public function getRoleByName($role_name = '') {
23
		$options = array(
24
			'type' => 'object',
25
			'subtype' => 'role',
26
			'metadata_name_value_pairs' => array(
27
				'name' => 'name',
28
				'value' => $role_name,
29
				'operand' => '=',
30
			),
31
			'limit' => 1,
32
		);
33
		$role_array = elgg_get_entities_from_metadata($options);
34
		return $role_array ? $role_array[0] : false;
35
	}
36
37
	/**
38
	 * {@inheritdoc}
39
	 */
40
	public function getUserRole(\ElggUser $user) {
41
		$options = array(
42
			'type' => 'object',
43
			'subtype' => 'role',
44
			'relationship' => 'has_role',
45
			'relationship_guid' => $user->guid,
46
			'limit' => 1,
47
		);
48
		$roles = elgg_get_entities_from_relationship($options);
49
		return $roles ? $roles[0] : false;
50
	}
51
52
	/**
53
	 * {@inheritdoc}
54
	 */
55
	public function setUserRole(\ElggUser $user, \ElggRole $role) {
56
		return (bool) add_entity_relationship($user->guid, 'has_role', $role->guid);
57
	}
58
59
	/**
60
	 * {@inheritdoc}
61
	 */
62
	public function unsetUserRole(\ElggUser $user) {
63
		return (bool) remove_entity_relationships($user->guid, 'has_role');
64
	}
65
66
}
67