Passed
Push — Submit-Comment-Mobile-API ( dc14cd...92a0ca )
by
unknown
89:49 queued 71:53
created

comment.php ➔ submit_comment()   F

Complexity

Conditions 17
Paths 313

Size

Total Lines 83
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 56
nc 313
nop 4
dl 0
loc 83
rs 3.7268
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
4
elgg_ws_expose_function(
5
	"get.commentsall",
6
	"get_comments_all",
7
	array(
8
		"user" => array('type' => 'string', 'required' => true),
9
		"guid" => array('type' => 'int', 'required' => true),
10
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
11
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
12
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
13
	),
14
	'Gets comments for given entity (guid), uses limit and offset.',
15
	'POST',
16
	true,
17
	false
18
);
19
20
elgg_ws_expose_function(
21
	"submit.comment",
22
	"submit_comment",
23
	array(
24
		"user" => array('type' => 'string', 'required' => true),
25
		"guid" => array('type' => 'int', 'required' => true),
26
		"message" => array('type' => 'string', 'required' => true),
27
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
28
	),
29
	'Gets comments for given entity (guid), uses limit and offset.',
30
	'POST',
31
	true,
32
	false
33
);
34
35
36
function get_comments_all($user, $guid, $limit, $offset, $lang)
37
{
38
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
39
	if (!$user_entity) {
40
		return "User was not found. Please try a different GUID, username, or email address";
41
	}
42
	if (!$user_entity instanceof ElggUser) {
43
		return "Invalid user. Please try a different GUID, username, or email address";
44
	}
45
	if (!elgg_is_logged_in()) {
46
		login($user_entity);
47
	}
48
49
	$entity = get_entity($guid);
50
	$subtype = 'nothing';
51
	if (!$entity) {
52
		return "Discussion was not found. Please try a different GUID";
53
	} else if (elgg_instanceof($entity, "object", "groupforumtopic")){
54
		$subtype = 'discussion_reply';
55
	} else {
56
		$subtype = 'comment';
57
	}
58
59
60
	$all_replies = elgg_list_entities_from_metadata(array(
61
		'type' => 'object',
62
		'subtype' => $subtype,
63
		'container_guid' => $guid,
64
		'limit' => $limit,
65
		'offset' => $offset
66
	));
67
	$replies = json_decode($all_replies);
68
	$comments = array();
69
	foreach ($replies as $reply) {
70
		$reply->userDetails = get_user_block($reply->owner_guid, $lang);
71
		$comments[] = $reply;
72
	}
73
74
	return $comments;
75
}
76
77
function submit_comment($user, $guid, $message, $lang)
78
{
79
	// No Empty Comments
80
	if (empty($message)){
81
		return elgg_echo("generic_comment:blank");
82
	}
83
84
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
85
	if (!$user_entity) {
86
		return "User was not found. Please try a different GUID, username, or email address";
87
	}
88
	if (!$user_entity instanceof ElggUser) {
89
		return "Invalid user. Please try a different GUID, username, or email address";
90
	}
91
	if (!elgg_is_logged_in()) {
92
		login($user_entity);
93
	}
94
95
	$entity = get_entity($guid);
96
	$subtype = 'nothing';
97
	if (!$entity) {
98
		return elgg_echo("generic_comment:notfound");
99
	} else if (elgg_instanceof($entity, "object", "groupforumtopic")){
100
		$subtype = 'discussion_reply';
101
		// Check if member of group
102
		$group = $entity->getContainerEntity();
103
		if (!$group->canWriteToContainer()) {
104
			return elgg_echo('groups:notmember');
105
		}
106
	} else {
107
		$subtype = 'comment';
108
	}
109
110
111
	$reply = '';
112
	if ($subtype == 'discussion_reply'){
113
		$reply = new ElggDiscussionReply();
114
	} else if ($subtype == 'comment'){
115
		$reply = new ElggComment();
116
	}
117
118
	$reply->description = $message;
119
	$reply->owner_guid = $user_entity->getGUID();
120
	$reply->container_guid =  $entity->getGUID();
121
	$reply->access_id = $entity->access_id; //idk
122
123
	$reply_guid = $reply->save();
124
	// If save comment fails, return message about it
125
	if ($reply_guid == false){
126
		if ($subtype == 'discussion_reply') {
127
			return elgg_echo('groupspost:failure');
128
		} else if ($subtype == 'comment'){
129
			return elgg_echo("generic_comment:failure");
130
		} else {
131
			return "reply failed - unknown - ref 00001 "; //this shouldnt happen
132
		}
133
	}
134
135
136
	//River Item
137
	if ($subtype == 'discussion_reply') {
138
		elgg_create_river_item(array(
139
		'view' => 'river/object/discussion_reply/create',
140
		'action_type' => 'reply',
141
		'subject_guid' => $user_entity->guid,
142
		'object_guid' => $reply_guid,
143
		'target_guid' => $entity->guid,
144
		));
145
		return elgg_echo('groupspost:success');
146
	} else if ($subtype == 'comment'){
147
		elgg_create_river_item(array(
148
			'view' => 'river/object/comment/create',
149
			'action_type' => 'comment',
150
			'subject_guid' => $user_entity->guid,
151
			'object_guid' => $reply_guid,
152
			'target_guid' => $entity->guid,
153
		));
154
		return elgg_echo('generic_comment:posted');
155
	}
156
157
	$result = "Should not reach this, but in-case, ref 00002";
158
	return $result;
159
}
160