Passed
Push — MobileAPI-PostDiscussion ( e6f536 )
by
unknown
23:04
created

discussion.php ➔ post_discussion()   F

Complexity

Conditions 26
Paths 824

Size

Total Lines 84
Code Lines 55

Duplication

Lines 12
Ratio 14.29 %

Importance

Changes 0
Metric Value
cc 26
eloc 55
nc 824
nop 8
dl 12
loc 84
rs 2.3809
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/*
3
 * Exposes API endpoints for Discussion entities
4
 */
5
6
elgg_ws_expose_function(
7
	"get.discussion",
8
	"get_discussion",
9
	array(
10
		"user" => array('type' => 'string', 'required' => true),
11
		"guid" => array('type' => 'int', 'required' => true),
12
		"thread" => array('type' => 'int', 'required' => false, 'default' => 1),
13
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
14
	),
15
	'Retrieves a discussion & all replies based on user id and discussion id',
16
	'POST',
17
	true,
18
	false
19
);
20
21
elgg_ws_expose_function(
22
	"get.discussions",
23
	"get_discussions",
24
	array(
25
		"user" => array('type' => 'string', 'required' => true),
26
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
27
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
28
		"filters" => array('type' => 'string', 'required' => false, 'default' => ""),
29
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
30
	),
31
	'Retrieves discussions based on user id',
32
	'POST',
33
	true,
34
	false
35
);
36
37
elgg_ws_expose_function(
38
 "post.discussion",
39
 "post_discussion",
40
 array(
41
	"user" => array('type' => 'string', 'required' => true),
42
	"title" => array('type' => 'string', 'required' => true),
43
	"message" => array('type' =>'string', 'required' => true),
44
	"container_guid" => array('type' =>'string', 'required' => false, 'default' => ''),
45
	"access" => array('type' =>'int', 'required' => false, 'default' => 2),
46
	"open" => array('type' =>'int', 'required' => false, 'default' => 1),
47
	"topic_guid" => array('type' =>'int', 'required' => false, 'default' => 0),
48
	"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
49
 ),
50
 'Posts/Saves a new discussion topic',
51
 'POST',
52
 true,
53
 false
54
);
55
56
function get_discussion($user, $guid, $thread, $lang)
57
{
58
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
59
	if (!$user_entity) {
60
		return "User was not found. Please try a different GUID, username, or email address";
61
	}
62
	if (!$user_entity instanceof ElggUser) {
63
		return "Invalid user. Please try a different GUID, username, or email address";
64
	}
65
66
	if (!elgg_is_logged_in()) {
67
		login($user_entity);
68
	}
69
70
	$entity = get_entity($guid);
71
	if (!$entity) {
72
		return "Discussion was not found. Please try a different GUID";
73
	}
74
	if (!elgg_instanceof($entity, "object", "groupforumtopic")) {
75
		return "Invalid discussion. Please try a different GUID";
76
	}
77
78
79
80
	$discussions = elgg_list_entities(array(
81
		'type' => 'object',
82
		'subtype' => 'groupforumtopic',
83
		'guid' => $guid
84
	));
85
	$discussion = json_decode($discussions)[0];
86
87
	$discussion->title = gc_explode_translation($discussion->title, $lang);
88
89
	$likes = elgg_get_annotations(array(
90
		'guid' => $discussion->guid,
91
		'annotation_name' => 'likes'
92
	));
93
	$discussion->likes = count($likes);
94
95
	$liked = elgg_get_annotations(array(
96
		'guid' => $discussion->guid,
97
		'annotation_owner_guid' => $user_entity->guid,
98
		'annotation_name' => 'likes'
99
	));
100
	$discussion->liked = count($liked) > 0;
101
102
	$discussion->userDetails = get_user_block($discussion->owner_guid, $lang);
103
	$discussion->description = gc_explode_translation($discussion->description, $lang);
104
105
	$discussionsArray = array();
106
	$discussionsArray[] = $discussion;
107
108
	if ($thread) {
109
		$all_replies = elgg_list_entities_from_metadata(array(
110
			'type' => 'object',
111
			'subtype' => 'discussion_reply',
112
			'container_guid' => $guid
113
		));
114
		$replies = json_decode($all_replies);
115
		$replies = array_reverse($replies);
116
117
		foreach ($replies as $reply) {
118
			$discussionsArray[] = $reply;
119
		}
120
	}
121
122
	return $discussionsArray;
123
}
124
125
function get_discussions($user, $limit, $offset, $filters, $lang)
126
{
127
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
128
	if (!$user_entity) {
129
		return "User was not found. Please try a different GUID, username, or email address";
130
	}
131
	if (!$user_entity instanceof ElggUser) {
132
		return "Invalid user. Please try a different GUID, username, or email address";
133
	}
134
135
	if (!elgg_is_logged_in()) {
136
		login($user_entity);
137
	}
138
139
	$filter_data = json_decode($filters);
140
	if (!empty($filter_data)) {
141
		$params = array(
142
			'type' => 'object',
143
			'subtype' => 'groupforumtopic',
144
			'limit' => $limit,
145
			'offset' => $offset
146
		);
147
148
		if ($filter_data->mine) {
149
			$params['owner_guid'] = $user_entity->guid;
150
		}
151
152 View Code Duplication
		if ($filter_data->name) {
153
			$db_prefix = elgg_get_config('dbprefix');
154
			$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
155
			$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
156
		}
157
158
		if ($filter_data->mine) {
159
			$all_discussions = elgg_list_entities_from_relationship($params);
160
		} else {
161
			$all_discussions = elgg_list_entities_from_metadata($params);
162
		}
163
	} else {
164
		$all_discussions = elgg_list_entities(array(
165
			'type' => 'object',
166
			'subtype' => 'groupforumtopic',
167
			'limit' => $limit,
168
			'offset' => $offset
169
		));
170
	}
171
172
	$discussions = json_decode($all_discussions);
173
174
	foreach ($discussions as $discussion) {
175
		$discussion->name = gc_explode_translation($discussion->name, $lang);
176
177
		$likes = elgg_get_annotations(array(
178
			'guid' => $discussion->guid,
179
			'annotation_name' => 'likes'
180
		));
181
		$discussion->likes = count($likes);
182
183
		$liked = elgg_get_annotations(array(
184
			'guid' => $discussion->guid,
185
			'annotation_owner_guid' => $user_entity->guid,
186
			'annotation_name' => 'likes'
187
		));
188
		$discussion->liked = count($liked) > 0;
189
190
		$discussion->userDetails = get_user_block($discussion->owner_guid, $lang);
191
		$discussion->description = gc_explode_translation($discussion->description, $lang);
192
	}
193
194
	return $discussions;
195
}
196
197
function post_discussion($user, $title, $message, $container_guid, $access, $open, $topic_guid, $lang)
198
{
199
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
200
		if (!$user_entity) {
201
			return "User was not found. Please try a different GUID, username, or email address";
202
		}
203
		if (!$user_entity instanceof ElggUser) {
204
			return "Invalid user. Please try a different GUID, username, or email address";
205
		}
206
		if (!elgg_is_logged_in()) {
207
			login($user_entity);
208
		}
209
210
		//check required fields
211
		$titles = json_decode($title);
212
		$message = json_decode($message);
213
		if (!$titles->en && !$titles->fr) { return elgg_echo("discussion:error:missing"); }
214
		if (!$message->en && !$message->fr) { return elgg_echo("discussion:error:missing");  }
215 View Code Duplication
		if (!($titles->en && $message->en) && !($titles->fr && $message->fr)) { return "require-same-lang"; }
216
217
		$container = get_entity($container_guid);
218
		if (!$container || !$container->canWriteToContainer(0, 'object', 'groupforumtopic')) {
219
			return elgg_echo('discussion:error:permissions');
220
		}
221
222
		//Check if new topic or edit
223
		$new_topic = true;
224
		if ($topic_guid > 0) {
225
		 $new_topic = false;
226
		}
227
228
		if ($new_topic) {
229
			$topic = new ElggObject();
230
			$topic->subtype = 'groupforumtopic';
231
		} else {
232
			$topic = get_entity($guid);
0 ignored issues
show
Bug introduced by
The variable $guid does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
233
			if (!elgg_instanceof($topic, 'object', 'groupforumtopic') || !$topic->canEdit()) {
234
				return elgg_echo('discussion:topic:notfound');
235
			}
236
		}
237
238
		//french english setup
239
		$title1 = htmlspecialchars($titles->en, ENT_QUOTES, 'UTF-8');
240
		$title2 = htmlspecialchars($titles->fr, ENT_QUOTES, 'UTF-8');
241
		$title =  gc_implode_translation($title1, $title2);
242
		$desc = gc_implode_translation($message->en, $message->fr);
243
244
		if ($access == 1 && !$container->isPublicMembership()){
245
			$access = 2; //Access cannot be public if group is not public. Default to group only.
246
		}
247
		$access_id = $access;
248
		if ($access_id === 2){
249
			$access_id = $container->group_acl; //Sets access id to match group only id.
250
		}
251
252
253
		$topic->title = $title;
254
		$topic->title2 = $title2;
255
		$topic->description = $desc;
256
		$topic->description2 = $message->fr;
257
		$topic->status = ($open == 1) ? "open" : "closed";
258
		$topic->access_id = $access_id;
259
		$topic->container_guid = $container_guid;
260
261
		$result = $topic->save();
262
		if (!$result) {
263
			return elgg_echo('discussion:error:notsaved');
264
		}
265
266
		//handle results differently for new topics and topic edits
267 View Code Duplication
		if ($new_topic) {
268
			system_message(elgg_echo('discussion:topic:created'));
269
			elgg_create_river_item(array(
270
				'view' => 'river/object/groupforumtopic/create',
271
				'action_type' => 'create',
272
				'subject_guid' => elgg_get_logged_in_user_guid(),
273
				'object_guid' => $topic->guid,
274
			));
275
		} else {
276
			system_message(elgg_echo('discussion:topic:updated'));
277
		}
278
279
		return $topic->getURL();
280
}
281