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

invitefriends_add_friends()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 5
nop 4
dl 0
loc 25
ccs 0
cts 14
cp 0
crap 30
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * Elgg invite friends
4
 *
5
 * @package ElggInviteFriends
6
 */
7
8
/**
9
 * Invite friend init
10
 *
11
 * @return void
12
 */
13
function invitefriends_init() {
14 31
	elgg_register_page_handler('invite', 'invitefriends_page_handler');
0 ignored issues
show
Deprecated Code introduced by
The function elgg_register_page_handler() has been deprecated: 3.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

14
	/** @scrutinizer ignore-deprecated */ elgg_register_page_handler('invite', 'invitefriends_page_handler');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
15
16 31
	elgg_register_plugin_hook_handler('register', 'user', 'invitefriends_add_friends');
17
18 31
	elgg_register_plugin_hook_handler('register', 'menu:page', 'invitefriends_register_page_menu');
19 31
}
20
21
/**
22
 * Page handler function
23
 *
24
 * @param array $page Page URL segments
25
 * @return bool
26
 */
27
function invitefriends_page_handler($page) {
0 ignored issues
show
Unused Code introduced by
The parameter $page is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

27
function invitefriends_page_handler(/** @scrutinizer ignore-unused */ $page) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
	echo elgg_view_resource('invitefriends/invite');
29
	return true;
30
}
31
32
/**
33
 * Adds menu items to the page menu
34
 *
35
 * @param string         $hook   'register'
36
 * @param string         $type   'menu:page'
37
 * @param ElggMenuItem[] $result Current items
38
 * @param array          $params Hook params
39
 *
40
 * @return void|ElggMenuItem[]
41
 */
42
function invitefriends_register_page_menu($hook, $type, $result, $params) {
3 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

42
function invitefriends_register_page_menu($hook, $type, $result, /** @scrutinizer ignore-unused */ $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

42
function invitefriends_register_page_menu($hook, /** @scrutinizer ignore-unused */ $type, $result, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

42
function invitefriends_register_page_menu(/** @scrutinizer ignore-unused */ $hook, $type, $result, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43 1
	if (!elgg_is_logged_in()) {
44 1
		return;
45
	}
46
	
47
	if (!elgg_get_config('allow_registration')) {
48
		return;
49
	}
50
	
51
	$result[] = \ElggMenuItem::factory([
52
		'name' => 'invite',
53
		'text' => elgg_echo('friends:invite'),
54
		'href' => 'invite',
55
		'contexts' => ['friends'],
56
	]);
57
	
58
	return $result;
59
}
60
61
/**
62
 * Add friends if invite code was set
63
 *
64
 * @param string $hook   'register'
65
 * @param string $type   'user'
66
 * @param bool   $result Whether to allow registration
67
 * @param array  $params Hook params
68
 *
69
 * @return void
70
 */
71
function invitefriends_add_friends($hook, $type, $result, $params) {
3 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

71
function invitefriends_add_friends($hook, /** @scrutinizer ignore-unused */ $type, $result, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

71
function invitefriends_add_friends(/** @scrutinizer ignore-unused */ $hook, $type, $result, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $result is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

71
function invitefriends_add_friends($hook, $type, /** @scrutinizer ignore-unused */ $result, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
72
	$user = elgg_extract('user', $params);
73
	if (!($user instanceof \ElggUser)) {
74
		return;
75
	}
76
	
77
	$friend_guid = elgg_extract('friend_guid', $params);
78
	$invite_code = elgg_extract('invitecode', $params);
79
	
80
	if (!$friend_guid) {
81
		return;
82
	}
83
	
84
	$friend_user = get_user($friend_guid);
85
	if (!($friend_user instanceof \ElggUser)) {
86
		return;
87
	}
88
89
	if (!elgg_validate_invite_code($friend_user->username, $invite_code)) {
90
		return;
91
	}
92
	
93
	// Make mutual friends
94
	$user->addFriend($friend_guid, true);
95
	$friend_user->addFriend($user->guid, true);
96
}
97
98
return function() {
99 18
	elgg_register_event_handler('init', 'system', 'invitefriends_init');
100
};
101