Completed
Push — Api_opt ( 47a261...24829b )
by
unknown
69:01 queued 36:29
created

opportunity.php ➔ get_opportunity()   F

Complexity

Conditions 29
Paths > 20000

Size

Total Lines 146
Code Lines 99

Duplication

Lines 30
Ratio 20.55 %

Importance

Changes 0
Metric Value
cc 29
eloc 99
nc 512024
nop 3
dl 30
loc 146
rs 2
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 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
elgg_ws_expose_function(
37
	"apply.post",
38
	"apply_post",
39
	array(
40
		"user" => array('type' => 'string', 'required' => true),
41
		"guid" => array('type' => 'int', 'required' => true),
42
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
43
	),
44
	'Retrieves a opportunity based on user id and opportunity id',
45
	'POST',
46
	true,
47
	false
48
);
49
50
function get_opportunity($user, $guid, $lang)
51
{
52
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
53
	if (!$user_entity) {
54
		return "User was not found. Please try a different GUID, username, or email address";
55
	}
56
	if (!$user_entity instanceof ElggUser) {
57
		return "Invalid user. Please try a different GUID, username, or email address";
58
	}
59
60
	if (!elgg_is_logged_in()) {
61
		login($user_entity);
62
	}
63
64
	$entity = get_entity($guid);
65
	if (!$entity) {
66
		return "Opportunity was not found. Please try a different GUID";
67
	}
68
	if (!elgg_instanceof($entity, 'object', 'mission')) {
69
		return "Invalid opportunity. Please try a different GUID";
70
	}
71
72
73
74
	$opportunities = elgg_list_entities(array(
75
		'type' => 'object',
76
		'subtype' => 'mission',
77
		'guid' => $guid
78
	));
79
	$opportunity = json_decode($opportunities)[0];
80
81
	$opportunity->title = gc_explode_translation($opportunity->title, $lang);
82
83
	$likes = elgg_get_annotations(array(
84
		'guid' => $opportunity->guid,
85
		'annotation_name' => 'likes'
86
	));
87
	$opportunity->likes = count($likes);
88
89
	$liked = elgg_get_annotations(array(
90
		'guid' => $opportunity->guid,
91
		'annotation_owner_guid' => $user_entity->guid,
92
		'annotation_name' => 'likes'
93
	));
94
	$opportunity->liked = count($liked) > 0;
95
96
	$opportunity->comments = get_entity_comments($opportunity->guid);
97
98
	$opportunity->userDetails = get_user_block($opportunity->owner_guid, $lang);
99
	$opportunity->description = gc_explode_translation($opportunity->description, $lang);
100
101
	$opportunityObj = get_entity($opportunity->guid);
102
	$opportunity->jobtype = elgg_echo($opportunityObj->job_type);
103
	$opportunity->roletype = elgg_echo($opportunityObj->role_type);
104
	//$opportunity->programArea = elgg_echo('missions:program_area') . ": " . elgg_echo($opportunityObj->program_area); //This should work and translate to user lang but doesnt
105
	$opportunity->programArea = elgg_echo($opportunityObj->program_area);
106
	$opportunity->numOpportunities = $opportunityObj->number;
107
	$opportunity->idealStart = $opportunityObj->start_date;
108
	$opportunity->idealComplete = $opportunityObj->complete_date;
109
	$opportunity->deadline = $opportunityObj->deadline;
110
	$opportunity->oppVirtual = $opportunityObj->remotely;
111
	$opportunity->oppOnlyIn = $opportunityObj->openess;
112
	$opportunity->location = elgg_echo($opportunityObj->location);
113
	$opportunity->security = elgg_echo($opportunityObj->security);
114
	$opportunity->skills = $opportunityObj->key_skills;
115
	//$opportunity->participants = $opportunityObj->;
116
	//$opportunity->applicants = $opportunityObj->;
117
	$opportunity->timezone = elgg_echo($opportunityObj->timezone);
118
	$opportunity->timecommitment = $opportunityObj->time_commitment;
119
	$opportunity->department = $opportunityObj->department;
120
121
	//Language metadata
122
	$unpacked_array = mm_unpack_mission($opportunityObj);
123
	$unpacked_language = '';
124 View Code Duplication
	if (! empty($unpacked_array['lwc_english']) || ! empty($unpacked_array['lwc_french'])) {
125
  	$unpacked_language .= '<b>' . elgg_echo('missions:written_comprehension') . ': </b>';
126
  	if (! empty($unpacked_array['lwc_english'])) {
127
      $unpacked_language .= '<span name="mission-lwc-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lwc_english'])) . '</span> ';
128
  	}
129
  	if (! empty($unpacked_array['lwc_french'])) {
130
      $unpacked_language .= '<span name="mission-lwc-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lwc_french'])) . '</span>';
131
  	}
132
		$unpacked_language .= '<br>';
133
	}
134 View Code Duplication
	if (! empty($unpacked_array['lwe_english']) || ! empty($unpacked_array['lwe_french'])) {
135
		$unpacked_language .= '<b>' . elgg_echo('missions:written_expression') . ': </b>';
136
		if (! empty($unpacked_array['lwe_english'])) {
137
	  	$unpacked_language .= '<span name="mission-lwe-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lwe_english'])) . '</span> ';
138
	 	}
139
	  if (! empty($unpacked_array['lwe_french'])) {
140
	  	$unpacked_language .= '<span name="mission-lwe-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lwe_french'])) . '</span>';
141
	  }
142
	  	$unpacked_language .= '<br>';
143
	}
144 View Code Duplication
	if (! empty($unpacked_array['lop_english']) || ! empty($unpacked_array['lop_french'])) {
145
		$unpacked_language .= '<b>' . elgg_echo('missions:oral_proficiency') . ': </b>';
146
		if (! empty($unpacked_array['lop_english'])) {
147
			$unpacked_language .= '<span name="mission-lop-english">' . elgg_echo('missions:formatted:english', array($unpacked_array['lop_english'])) . '</span> ';
148
		}
149
		if (! empty($unpacked_array['lop_french'])) {
150
			$unpacked_language .= '<span name="mission-lop-french">' . elgg_echo('missions:formatted:french', array($unpacked_array['lop_french'])) . '</span>';
151
		}
152
		$unpacked_language .= '<br>';
153
	}
154
	if (empty($unpacked_language)) {
155
		$unpacked_language = '<span name="no-languages">' . elgg_echo('missions:none_required') . '</span>';
156
	}
157
	$opportunity->languageRequirements = $unpacked_language;
158
159
	//scheduling metadata
160
	$unpacked_time = '';
161
	if ($opportunityObj->mon_start) {
162
		$unpacked_time .= '<b>' . elgg_echo('missions:mon') . ': </b>';
163
	 	$unpacked_time .= $opportunityObj->mon_start . elgg_echo('missions:to') .  $opportunityObj->mon_duration . '<br>';
164
	}
165
	if ($opportunityObj->tue_start) {
166
		$unpacked_time .= '<b>' . elgg_echo('missions:tue') . ': </b>';
167
		$unpacked_time .= '<span name="mission-tue-start">' . $opportunityObj->tue_start . '</span>' . elgg_echo('missions:to') . '<span name="mission-tue-duration">' . $opportunityObj->tue_duration . '</span><br>';
168
	}
169
	if ($opportunityObj->wed_start) {
170
		$unpacked_time .= '<b>' . elgg_echo('missions:wed') . ': </b>';
171
		$unpacked_time .=  $opportunityObj->wed_start  . elgg_echo('missions:to') . $opportunityObj->wed_duration . '<br>';
172
	}
173
	if ($opportunityObj->thu_start) {
174
		$unpacked_time .= '<b>' . elgg_echo('missions:thu') . ': </b>';
175
		$unpacked_time .= '<span name="mission-thu-start">' . $opportunityObj->thu_start . '</span>' . elgg_echo('missions:to') . '<span name="mission-thu-duration">' . $opportunityObj->thu_duration . '</span><br>';
176
	}
177
	if ($opportunityObj->fri_start) {
178
	  $unpacked_time .= '<b>' . elgg_echo('missions:fri') . ': </b>';
179
	  $unpacked_time .= '<span name="mission-fri-start">' . $opportunityObj->fri_start . '</span>' . elgg_echo('missions:to') . '<span name="mission-fri-duration">' . $opportunityObj->fri_duration . '</span><br>';
180
	}
181
	if ($opportunityObj->sat_start) {
182
	  $unpacked_time .= '<b>' . elgg_echo('missions:sat') . ': </b>';
183
	  $unpacked_time .= '<span name="mission-sat-start">' . $opportunityObj->sat_start . '</span>' . elgg_echo('missions:to') . '<span name="mission-sat-duration">' . $opportunityObj->sat_duration . '</span><br>';
184
	}
185
	if ($opportunityObj->sun_start) {
186
	  $unpacked_time .= '<b>' . elgg_echo('missions:sun') . ': </b>';
187
	  $unpacked_time .= '<span name="mission-sun-start">' . $opportunityObj->sun_start . '</span>' . elgg_echo('missions:to') . '<span name="mission-sun-duration">' . $opportunityObj->sun_duration . '</span><br>';
188
	}
189
	if (empty($unpacked_time)) {
190
	  $unpacked_time = '<span name="no-times">' . elgg_echo('missions:none_required') . '</span>';
191
	}
192
	$opportunity->schedulingRequirements = $unpacked_time;
193
194
	return $opportunity;
195
}
196
197
function get_opportunities($user, $limit, $offset, $filters, $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
207
	if (!elgg_is_logged_in()) {
208
		login($user_entity);
209
	}
210
211
	$filter_data = json_decode($filters);
212
	if (!empty($filter_data)) {
213
		$params = array(
214
			'type' => 'object',
215
			'subtype' => 'mission',
216
			'limit' => $limit,
217
			'offset' => $offset
218
		);
219
220
		if ($filter_data->type) {
221
			$params['metadata_name'] = 'job_type';
222
			$params['metadata_value'] = $filter_data->type;
223
		}
224
225 View Code Duplication
		if ($filter_data->name) {
226
			$db_prefix = elgg_get_config('dbprefix');
227
			$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
228
			$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
229
		}
230
231
		if ($filter_data->mine) {
232
			$all_opportunities = elgg_list_entities_from_relationship($params);
233
		} else {
234
			$all_opportunities = elgg_list_entities_from_metadata($params);
235
		}
236
	} else {
237
		$all_opportunities = elgg_list_entities(array(
238
			'type' => 'object',
239
			'subtype' => 'mission',
240
			'limit' => $limit,
241
			'offset' => $offset
242
		));
243
	}
244
245
	$opportunities = json_decode($all_opportunities);
246
247
	foreach ($opportunities as $opportunity) {
248
		$opportunity->title = gc_explode_translation($opportunity->title, $lang);
249
250
		$likes = elgg_get_annotations(array(
251
			'guid' => $opportunity->guid,
252
			'annotation_name' => 'likes'
253
		));
254
		$opportunity->likes = count($likes);
255
256
		$liked = elgg_get_annotations(array(
257
			'guid' => $opportunity->guid,
258
			'annotation_owner_guid' => $user_entity->guid,
259
			'annotation_name' => 'likes'
260
		));
261
		
262
		$opportunity->liked = count($liked) > 0;
263
264
		$opportunityObj = get_entity($opportunity->guid);
265
		$opportunity->jobtype = elgg_echo($opportunityObj->job_type);
266
		$opportunity->roletype = elgg_echo($opportunityObj->role_type);
267
		$opportunity->deadline = $opportunityObj->deadline;
268
		$opportunity->programArea = elgg_echo($opportunityObj->program_area);
269
		$opportunity->owner = ($opportunityObj->getOwnerEntity() == $user_entity);
270
		$opportunity->iconURL = $opportunityObj->getIconURL();
271
272
		$opportunity->userDetails = get_user_block($opportunity->owner_guid, $lang);
273
		$opportunity->description = clean_text(gc_explode_translation($opportunity->description, $lang));
274
		$opportunity->state = $opportunityObj->state;
275
	}
276
277
	return $opportunities;
278
}
279
280
function apply_post($user, $guid, $lang)
281
{
282
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
283
	if (!$user_entity) {
284
		return "User was not found. Please try a different GUID, username, or email address";
285
	}
286
	if (!$user_entity instanceof ElggUser) {
287
		return "Invalid user. Please try a different GUID, username, or email address";
288
	}
289
290
	if (!elgg_is_logged_in()) {
291
		login($user_entity);
292
	}
293
294
	$entity = get_entity($guid);
295
	if (!$entity) {
296
		return "Opportunity was not found. Please try a different GUID";
297
	}
298
	if (!elgg_instanceof($entity, 'object', 'mission')) {
299
		return "Invalid opportunity. Please try a different GUID";
300
	}
301
302
	// Creates an applied relationship between user and mission if there is no relationship there already.
303 View Code Duplication
	if(!check_entity_relationship($entity->guid, 'mission_accepted', $user_entity->guid) && !check_entity_relationship($entity->guid, 'mission_tentative', $user_entity->guid)) {
304
		add_entity_relationship($entity->guid, 'mission_applied', $user_entity->guid);
305
		$message = 'Apply';
306
	}
307
		
308
	// Opt in applicant if they are not opted in yet.
309
	if(!check_if_opted_in($user_entity)) {
310
		$user_entity->opt_in_missions = 'gcconnex_profile:opt:yes';
311
		$user_entity->save();
312
	}
313
314
	return 'You '.$message;
0 ignored issues
show
Bug introduced by
The variable $message does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
315
316
}