Completed
Pull Request — gcconnex (#1509)
by Nick
18:12
created

blog.php ➔ get_blogpost()   B

Complexity

Conditions 8
Paths 28

Size

Total Lines 55
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 34
nc 28
nop 3
dl 0
loc 55
rs 7.4033
c 0
b 0
f 0

How to fix   Long Method   

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 Blog entities
4
 */
5
6
elgg_ws_expose_function(
7
	"get.blogpost",
8
	"get_blogpost",
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 blog post & all replies based on user id and blog post id',
15
	'POST',
16
	true,
17
	false
18
);
19
20
elgg_ws_expose_function(
21
	"get.blogposts",
22
	"get_blogposts",
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 blog posts & all replies based on user id',
31
	'POST',
32
	true,
33
	false
34
);
35
36
function get_blogpost($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 (!isset($entity)) {
48
		return "Blog was not found. Please try a different GUID";
49
	}
50
51
	if (!elgg_is_logged_in()) {
52
		login($user_entity);
53
	}
54
55
	$blog_posts = elgg_list_entities(array(
56
		'type' => 'object',
57
		'subtype' => 'blog',
58
		'guid' => $guid
59
	));
60
	$blog_post = json_decode($blog_posts)[0];
61
62
	$blog_post->title = gc_explode_translation($blog_post->title, $lang);
63
	$blog_post->description = gc_explode_translation($blog_post->description, $lang);
64
65
	$likes = elgg_get_annotations(array(
66
		'guid' => $blog_post->guid,
67
		'annotation_name' => 'likes'
68
	));
69
	$blog_post->likes = count($likes);
70
71
	$liked = elgg_get_annotations(array(
72
		'guid' => $blog_post->guid,
73
		'annotation_owner_guid' => $user_entity->guid,
74
		'annotation_name' => 'likes'
75
	));
76
	$blog_post->liked = count($liked) > 0;
77
78
	$blog_post->comments = get_entity_comments($blog_post->guid);
79
80
	$blog_post->userDetails = get_user_block($blog_post->owner_guid, $lang);
81
82
	$group = get_entity($blog_post->container_guid);
83
	$blog_post->group = gc_explode_translation($group->name, $lang);
84
85
	if (is_callable(array($group, 'getURL'))) {
86
		$blog_post->groupURL = $group->getURL();
87
	}
88
89
	return $blog_post;
90
}
91
92
function get_blogposts($user, $limit, $offset, $filters, $lang)
93
{
94
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
95
	if (!$user_entity) {
96
		return "User was not found. Please try a different GUID, username, or email address";
97
	}
98
	if (!$user_entity instanceof ElggUser) {
99
		return "Invalid user. Please try a different GUID, username, or email address";
100
	}
101
102
	if (!elgg_is_logged_in()) {
103
		login($user_entity);
104
	}
105
106
	$filter_data = json_decode($filters);
107
	if (!empty($filter_data)) {
108
		$params = array(
109
			'type' => 'object',
110
			'subtype' => 'blog',
111
			'limit' => $limit,
112
			'offset' => $offset
113
		);
114
115
		if ($filter_data->name) {
116
			$db_prefix = elgg_get_config('dbprefix');
117
			$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
118
			$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
119
		}
120
121
		$all_blog_posts = elgg_list_entities_from_metadata($params);
122
	} else {
123
		$all_blog_posts = elgg_list_entities(array(
124
			'type' => 'object',
125
			'subtype' => 'blog',
126
			'limit' => $limit,
127
			'offset' => $offset
128
		));
129
	}
130
131
	$blog_posts = json_decode($all_blog_posts);
132
133
	foreach ($blog_posts as $blog_post) {
134
		$blog_post->title = gc_explode_translation($blog_post->title, $lang);
135
		$blog_post->description = gc_explode_translation($blog_post->description, $lang);
136
137
		$likes = elgg_get_annotations(array(
138
			'guid' => $blog_post->guid,
139
			'annotation_name' => 'likes'
140
		));
141
		$blog_post->likes = count($likes);
142
143
		$liked = elgg_get_annotations(array(
144
			'guid' => $blog_post->guid,
145
			'annotation_owner_guid' => $user_entity->guid,
146
			'annotation_name' => 'likes'
147
		));
148
		$blog_post->liked = count($liked) > 0;
149
150
		$blog_post->comments = get_entity_comments($blog_post->guid);
151
152
		$blog_post->userDetails = get_user_block($blog_post->owner_guid, $lang);
153
154
		$group = get_entity($blog_post->container_guid);
155
		$blog_post->group = gc_explode_translation($group->name, $lang);
156
157
		if (is_callable(array($group, 'getURL'))) {
158
			$blog_post->groupURL = $group->getURL();
159
		}
160
	}
161
162
	return $blog_posts;
163
}
164