Completed
Pull Request — master (#1890)
by
unknown
34:06 queued 16:01
created

doc.php ➔ get_docs()   C

Complexity

Conditions 10
Paths 80

Size

Total Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 80
nop 5
dl 0
loc 74
rs 6.7006
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 doc entities
4
 */
5
6
elgg_ws_expose_function(
7
	"get.doc",
8
	"get_doc",
9
	array(
10
		"user" => array('type' => 'string', 'required' => true),
11
		"guid" => array('type' => 'int', 'required' => true),
12
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
13
	),
14
	'Retrieves a Doc based on user id and doc id',
15
	'POST',
16
	true,
17
	false
18
);
19
20
elgg_ws_expose_function(
21
	"get.docs",
22
	"get_docs",
23
	array(
24
		"user" => array('type' => 'string', 'required' => true),
25
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
26
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
27
		"filters" => array('type' => 'string', 'required' => false, 'default' => ""),
28
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
29
	),
30
	'Retrieves Docs based on user id',
31
	'POST',
32
	true,
33
	false
34
);
35
36
function get_doc($user, $guid, $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
46
	$entity = get_entity($guid);
47
	if (!$entity) {
48
		return "Doc was not found. Please try a different GUID";
49
	}
50
	if (!elgg_instanceof($entity, 'object', 'etherpad')) {
51
		return "Invalid Doc. Please try a different GUID";
52
	}
53
54
	if (!elgg_is_logged_in()) {
55
		login($user_entity);
56
	}
57
58
	$docs = elgg_list_entities(array(
59
		'type' => 'object',
60
		'subtypes' => array('etherpad', 'subpad'),
61
		'guid' => $guid
62
	));
63
	$doc = json_decode($docs)[0];
64
65
	$doc->name = gc_explode_translation($doc->name, $lang);
66
67
	$likes = elgg_get_annotations(array(
68
		'guid' => $doc->guid,
69
		'annotation_name' => 'likes'
70
	));
71
	$doc->likes = count($likes);
72
73
	$liked = elgg_get_annotations(array(
74
		'guid' => $doc->guid,
75
		'annotation_owner_guid' => $user_entity->guid,
76
		'annotation_name' => 'likes'
77
	));
78
	$doc->liked = count($liked) > 0;
79
80
	$doc->comments = get_entity_comments($doc->guid);
81
	$doc->url = $entity->getPadPath();
82
83
	$doc->userDetails = get_user_block($doc->owner_guid, $lang);
84
	$doc->description = clean_text(gc_explode_translation($doc->description, $lang));
85
86
	return $doc;
87
}
88
89
function get_docs($user, $limit, $offset, $filters, $lang)
90
{
91
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
92
	if (!$user_entity) {
93
		return "User was not found. Please try a different GUID, username, or email address";
94
	}
95
	if (!$user_entity instanceof ElggUser) {
96
		return "Invalid user. Please try a different GUID, username, or email address";
97
	}
98
99
	if (!elgg_is_logged_in()) {
100
		login($user_entity);
101
	}
102
103
	$filter_data = json_decode($filters);
104
	if (!empty($filter_data)) {
105
		$params = array(
106
			'type' => 'object',
107
			'subtypes' => array('etherpad', 'subpad'),
108
			'limit' => $limit,
109
			'offset' => $offset
110
		);
111
112
		if ($filter_data->name) {
113
			$db_prefix = elgg_get_config('dbprefix');
114
			$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
115
			$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
116
		}
117
118
		$all_docs = elgg_list_entities_from_metadata($params);
119
	} else {
120
		$all_docs = elgg_list_entities(array(
121
			'type' => 'object',
122
			'subtypes' => array('etherpad', 'subpad'),
123
			'limit' => $limit,
124
			'offset' => $offset
125
		));
126
	}
127
128
	$docs = json_decode($all_docs);
129
130
	foreach ($docs as $doc) {
131
		$doc->name = gc_explode_translation($doc->name, $lang);
132
133
		$likes = elgg_get_annotations(array(
134
			'guid' => $doc->guid,
135
			'annotation_name' => 'likes'
136
		));
137
		$doc->likes = count($likes);
138
139
		$liked = elgg_get_annotations(array(
140
			'guid' => $doc->guid,
141
			'annotation_owner_guid' => $user_entity->guid,
142
			'annotation_name' => 'likes'
143
		));
144
		$doc->liked = count($liked) > 0;
145
146
		$docObj = new ElggPad($doc->guid);
147
		$doc->owner = ($docObj->getOwnerEntity() == $user_entity);
148
		$doc->iconURL = $docObj->getIconURL();
149
		$doc->url = $docObj->getPadPath();
150
151
		$doc->userDetails = get_user_block($doc->owner_guid, $lang);
152
		$doc->description = clean_text(gc_explode_translation($doc->description, $lang));
153
154
		if ($doc->container_guid != $doc->owner_guid){
155
			$doc->group_guid = $doc->container_guid;
156
			$docGroup = get_entity($doc->group_guid);
157
			$doc->group = gc_explode_translation($docGroup->name, $lang);
158
		  }
159
	}
160
161
	return $docs;
162
}
163