Completed
Pull Request — gcconnex (#1725)
by
unknown
19:45
created

opportunity.php ➔ get_opportunity()   B

Complexity

Conditions 8
Paths 32

Size

Total Lines 53
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

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