Completed
Pull Request — gcconnex (#1558)
by
unknown
15:36
created

discussion.php ➔ get_discussions()   C

Complexity

Conditions 11
Paths 152

Size

Total Lines 71
Code Lines 46

Duplication

Lines 31
Ratio 43.66 %

Importance

Changes 0
Metric Value
cc 11
eloc 46
nc 152
nop 5
dl 31
loc 71
rs 5.3174
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
 * 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' => 0),
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
function get_discussion($user, $guid, $thread, $lang)
38
{
39
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
40
	if (!$user_entity) {
41
		return "User was not found. Please try a different GUID, username, or email address";
42
	}
43
	if (!$user_entity instanceof ElggUser) {
44
		return "Invalid user. Please try a different GUID, username, or email address";
45
	}
46
47
	$entity = get_entity($guid);
48
	if (!$entity) {
49
		return "Discussion was not found. Please try a different GUID";
50
	}
51
	if (!elgg_instanceof($entity, "object", "groupforumtopic")) {
52
		return "Invalid discussion. Please try a different GUID";
53
	}
54
55
	if (!elgg_is_logged_in()) {
56
		login($user_entity);
57
	}
58
59
	$discussions = elgg_list_entities(array(
60
		'type' => 'object',
61
		'subtype' => 'groupforumtopic',
62
		'guid' => $guid
63
	));
64
	$discussion = json_decode($discussions)[0];
65
66
	$discussion->name = gc_explode_translation($discussion->name, $lang);
67
68
	$likes = elgg_get_annotations(array(
69
		'guid' => $discussion->guid,
70
		'annotation_name' => 'likes'
71
	));
72
	$discussion->likes = count($likes);
73
74
	$liked = elgg_get_annotations(array(
75
		'guid' => $discussion->guid,
76
		'annotation_owner_guid' => $user_entity->guid,
77
		'annotation_name' => 'likes'
78
	));
79
	$discussion->liked = count($liked) > 0;
80
81
	$discussion->userDetails = get_user_block($discussion->owner_guid, $lang);
82
	$discussion->description = clean_text(gc_explode_translation($discussion->description, $lang));
83
84
	$discussionsArray = array();
85
	$discussionsArray[] = $discussion;
86
87
	if ($thread) {
88
		$all_replies = elgg_list_entities_from_metadata(array(
89
			'type' => 'object',
90
			'subtype' => 'discussion_reply',
91
			'container_guid' => $guid
92
		));
93
		$replies = json_decode($all_replies);
94
		$replies = array_reverse($replies);
95
96
		foreach ($replies as $reply) {
97
			$discussionsArray[] = $reply;
98
		}
99
	}
100
101
	return $discussionsArray;
102
}
103
104
function get_discussions($user, $limit, $offset, $filters, $lang)
105
{
106
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
107
	if (!$user_entity) {
108
		return "User was not found. Please try a different GUID, username, or email address";
109
	}
110
	if (!$user_entity instanceof ElggUser) {
111
		return "Invalid user. Please try a different GUID, username, or email address";
112
	}
113
114
	if (!elgg_is_logged_in()) {
115
		login($user_entity);
116
	}
117
118
	$filter_data = json_decode($filters);
119 View Code Duplication
	if (!empty($filter_data)) {
120
		$params = array(
121
			'type' => 'object',
122
			'subtype' => 'groupforumtopic',
123
			'limit' => $limit,
124
			'offset' => $offset
125
		);
126
127
		if ($filter_data->mine) {
128
			$params['owner_guid'] = $user_entity->guid;
129
		}
130
131
		if ($filter_data->name) {
132
			$db_prefix = elgg_get_config('dbprefix');
133
			$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
134
			$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
135
		}
136
137
		if ($filter_data->mine) {
138
			$all_discussions = elgg_list_entities_from_relationship($params);
139
		} else {
140
			$all_discussions = elgg_list_entities_from_metadata($params);
141
		}
142
	} else {
143
		$all_discussions = elgg_list_entities(array(
144
			'type' => 'object',
145
			'subtype' => 'groupforumtopic',
146
			'limit' => $limit,
147
			'offset' => $offset
148
		));
149
	}
150
151
	$discussions = json_decode($all_discussions);
152
153
	foreach ($discussions as $discussion) {
154
		$discussion->name = gc_explode_translation($discussion->name, $lang);
155
156
		$likes = elgg_get_annotations(array(
157
			'guid' => $discussion->guid,
158
			'annotation_name' => 'likes'
159
		));
160
		$discussion->likes = count($likes);
161
162
		$liked = elgg_get_annotations(array(
163
			'guid' => $discussion->guid,
164
			'annotation_owner_guid' => $user_entity->guid,
165
			'annotation_name' => 'likes'
166
		));
167
		$discussion->liked = count($liked) > 0;
168
169
		$discussion->userDetails = get_user_block($discussion->owner_guid, $lang);
170
		$discussion->description = clean_text(gc_explode_translation($discussion->description, $lang));
171
	}
172
173
	return $discussions;
174
}
175