Completed
Push — preprod ( 503287 )
by Ilia
29:11
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
		$likes = elgg_get_annotations(array(
71
			'guid' => $reply->guid,
72
			'annotation_name' => 'likes'
73
		));
74
		$reply->likes = count($likes);
75
76
		$liked = elgg_get_annotations(array(
77
			'guid' => $reply->guid,
78
			'annotation_owner_guid' => $user_entity->guid,
79
			'annotation_name' => 'likes'
80
		));
81
		$reply->liked = count($liked) > 0;
82
83
		$reply->userDetails = get_user_block($reply->owner_guid, $lang);
84
		$comments[] = $reply;
85
	}
86
87
	return $comments;
88
}
89
90
function submit_comment($user, $guid, $message, $lang)
91
{
92
	// No Empty Comments
93
	if (empty($message)){
94
		return elgg_echo("generic_comment:blank");
95
	}
96
97
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
98
	if (!$user_entity) {
99
		return "User was not found. Please try a different GUID, username, or email address";
100
	}
101
	if (!$user_entity instanceof ElggUser) {
102
		return "Invalid user. Please try a different GUID, username, or email address";
103
	}
104
	if (!elgg_is_logged_in()) {
105
		login($user_entity);
106
	}
107
108
	$entity = get_entity($guid);
109
	$subtype = 'nothing';
110
	if (!$entity) {
111
		return elgg_echo("generic_comment:notfound");
112
	} else if (elgg_instanceof($entity, "object", "groupforumtopic")){
113
		$subtype = 'discussion_reply';
114
		// Check if member of group
115
		$group = $entity->getContainerEntity();
116
		if (!$group->canWriteToContainer()) {
117
			return elgg_echo('groups:notmember');
118
		}
119
	} else {
120
		$subtype = 'comment';
121
	}
122
123
124
	$reply = '';
125
	if ($subtype == 'discussion_reply'){
126
		$reply = new ElggDiscussionReply();
127
	} else if ($subtype == 'comment'){
128
		$reply = new ElggComment();
129
	}
130
131
	$reply->description = $message;
132
	$reply->owner_guid = $user_entity->getGUID();
133
	$reply->container_guid =  $entity->getGUID();
134
	$reply->access_id = $entity->access_id; //idk
135
136
	$reply_guid = $reply->save();
137
	// If save comment fails, return message about it
138
	if ($reply_guid == false){
139
		if ($subtype == 'discussion_reply') {
140
			return elgg_echo('groupspost:failure');
141
		} else if ($subtype == 'comment'){
142
			return elgg_echo("generic_comment:failure");
143
		} else {
144
			return "reply failed - unknown - ref 00001 "; //this shouldnt happen
145
		}
146
	}
147
148
149
	//River Item
150
	if ($subtype == 'discussion_reply') {
151
		elgg_create_river_item(array(
152
		'view' => 'river/object/discussion_reply/create',
153
		'action_type' => 'reply',
154
		'subject_guid' => $user_entity->guid,
155
		'object_guid' => $reply_guid,
156
		'target_guid' => $entity->guid,
157
		));
158
		return elgg_echo('groupspost:success');
159
	} else if ($subtype == 'comment'){
160
		elgg_create_river_item(array(
161
			'view' => 'river/object/comment/create',
162
			'action_type' => 'comment',
163
			'subject_guid' => $user_entity->guid,
164
			'object_guid' => $reply_guid,
165
			'target_guid' => $entity->guid,
166
		));
167
		return elgg_echo('generic_comment:posted');
168
	}
169
170
	$result = "Should not reach this, but in-case, ref 00002";
171
	return $result;
172
}
173