Completed
Push — Api_opt ( 52bf6b )
by
unknown
24:42
created

opportunity.php ➔ apply_post()   C

Complexity

Conditions 11
Paths 56

Size

Total Lines 65
Code Lines 20

Duplication

Lines 4
Ratio 6.15 %

Importance

Changes 0
Metric Value
cc 11
eloc 20
nc 56
nop 3
dl 4
loc 65
rs 5.9999
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
	return $opportunity;
102
}
103
104
function get_opportunities($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
	if (!empty($filter_data)) {
120
		$params = array(
121
			'type' => 'object',
122
			'subtype' => 'mission',
123
			'limit' => $limit,
124
			'offset' => $offset
125
		);
126
127
		if ($filter_data->type) {
128
			$params['metadata_name'] = 'job_type';
129
			$params['metadata_value'] = $filter_data->type;
130
		}
131
132
		if ($filter_data->name) {
133
			$db_prefix = elgg_get_config('dbprefix');
134
			$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
135
			$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
136
		}
137
138
		if ($filter_data->mine) {
139
			$all_opportunities = elgg_list_entities_from_relationship($params);
140
		} else {
141
			$all_opportunities = elgg_list_entities_from_metadata($params);
142
		}
143
	} else {
144
		$all_opportunities = elgg_list_entities(array(
145
			'type' => 'object',
146
			'subtype' => 'mission',
147
			'limit' => $limit,
148
			'offset' => $offset
149
		));
150
	}
151
152
	$opportunities = json_decode($all_opportunities);
153
154
	foreach ($opportunities as $opportunity) {
155
		$opportunity->title = gc_explode_translation($opportunity->title, $lang);
156
157
		$likes = elgg_get_annotations(array(
158
			'guid' => $opportunity->guid,
159
			'annotation_name' => 'likes'
160
		));
161
		$opportunity->likes = count($likes);
162
163
		$liked = elgg_get_annotations(array(
164
			'guid' => $opportunity->guid,
165
			'annotation_owner_guid' => $user_entity->guid,
166
			'annotation_name' => 'likes'
167
		));
168
		$opportunity->liked = count($liked) > 0;
169
170
		$opportunityObj = get_entity($opportunity->guid);
171
		$opportunity->owner = ($opportunityObj->getOwnerEntity() == $user_entity);
172
		$opportunity->iconURL = $opportunityObj->getIconURL();
173
174
		$opportunity->userDetails = get_user_block($opportunity->owner_guid, $lang);
175
		$opportunity->description = clean_text(gc_explode_translation($opportunity->description, $lang));
176
	}
177
178
	return $opportunities;
179
}
180
181
function apply_post($user, $guid, $lang)
182
{
183
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
184
	if (!$user_entity) {
185
		return "User was not found. Please try a different GUID, username, or email address";
186
	}
187
	if (!$user_entity instanceof ElggUser) {
188
		return "Invalid user. Please try a different GUID, username, or email address";
189
	}
190
191
	if (!elgg_is_logged_in()) {
192
		login($user_entity);
193
	}
194
195
	$entity = get_entity($guid);
196
	if (!$entity) {
197
		return "Opportunity was not found. Please try a different GUID";
198
	}
199
	if (!elgg_instanceof($entity, 'object', 'mission')) {
200
		return "Invalid opportunity. Please try a different GUID";
201
	}
202
203
	// Creates an applied relationship between user and mission if there is no relationship there already.
204 View Code Duplication
	if(!check_entity_relationship($entity->guid, 'mission_accepted', $user_entity->guid) && !check_entity_relationship($entity->guid, 'mission_tentative', $user_entity->guid)) {
205
		add_entity_relationship($entity->guid, 'mission_applied', $user_entity->guid);
206
		$message = 'Apply';
207
	}
208
		
209
	// Opt in applicant if they are not opted in yet.
210
	if(!check_if_opted_in($user_entity)) {
211
		$user_entity->opt_in_missions = 'gcconnex_profile:opt:yes';
212
		$user_entity->save();
213
	}
214
215
	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...
216
217
	// $opportunities = elgg_list_entities(array(
218
	// 	'type' => 'object',
219
	// 	'subtype' => 'mission',
220
	// 	'guid' => $guid
221
	// ));
222
	// $opportunity = json_decode($opportunities)[0];
223
224
	// $opportunity->title = gc_explode_translation($opportunity->title, $lang);
225
226
	// $likes = elgg_get_annotations(array(
227
	// 	'guid' => $opportunity->guid,
228
	// 	'annotation_name' => 'likes'
229
	// ));
230
	// $opportunity->likes = count($likes);
231
232
	// $liked = elgg_get_annotations(array(
233
	// 	'guid' => $opportunity->guid,
234
	// 	'annotation_owner_guid' => $user_entity->guid,
235
	// 	'annotation_name' => 'likes'
236
	// ));
237
	// $opportunity->liked = count($liked) > 0;
238
239
	// $opportunity->comments = get_entity_comments($opportunity->guid);
240
241
	// $opportunity->userDetails = get_user_block($opportunity->owner_guid, $lang);
242
	// $opportunity->description = gc_explode_translation($opportunity->description, $lang);
243
244
	//return $opportunity;
245
}