Total Complexity | 75 |
Total Lines | 599 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Likes often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Likes, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Likes |
||
23 | { |
||
24 | /** |
||
25 | * @var boolean Know if a request comes from an ajax call or not, depends on $_GET['js'] been set. |
||
26 | */ |
||
27 | protected $_js = false; |
||
28 | |||
29 | /** |
||
30 | * @var string If filled, its value will contain a string matching a key on a language var $txt[$this->_error] |
||
31 | */ |
||
32 | protected $_error = false; |
||
33 | |||
34 | /** |
||
35 | * @var string The unique type to like, needs to be unique and it needs to be no longer than 6 characters, only numbers and letters are allowed. |
||
36 | */ |
||
37 | protected $_type = ''; |
||
38 | |||
39 | /** |
||
40 | * @var string A generic string used if you need to pass any extra info. It gets set via $_GET['extra']. |
||
41 | */ |
||
42 | protected $_extra = false; |
||
43 | |||
44 | /** |
||
45 | * @var integer a valid ID to identify your like content. |
||
46 | */ |
||
47 | protected $_content = 0; |
||
48 | |||
49 | /** |
||
50 | * @var integer The number of times your content has been liked. |
||
51 | */ |
||
52 | protected $_numLikes = 0; |
||
53 | |||
54 | /** |
||
55 | * @var boolean If the current user has already liked this content. |
||
56 | */ |
||
57 | protected $_alreadyLiked = false; |
||
58 | |||
59 | /** |
||
60 | * @var array $_validLikes mostly used for external integration, needs to be filled as an array with the following keys: |
||
61 | * => 'can_like' boolean|string whether or not the current user can actually like your content. |
||
62 | * for can_like: Return a boolean true if the user can, otherwise return a string, the string will be used as key in a regular $txt language error var. The code assumes you already loaded your language file. If no value is returned or the $txt var isn't set, the code will use a generic error message. |
||
63 | * => 'redirect' string To add support for non JS users, It is highly encouraged to set a valid URL to redirect the user to, if you don't provide any, the code will redirect the user to the main page. The code only performs a light check to see if the redirect is valid so be extra careful while building it. |
||
64 | * => 'type' string 6 letters or numbers. The unique identifier for your content, the code doesn't check for duplicate entries, if there are 2 or more exact hook calls, the code will take the first registered one so make sure you provide a unique identifier. Must match with what you sent in $_GET['ltype']. |
||
65 | * => 'flush_cache' boolean this is optional, it tells the code to reset your like content's cache entry after a new entry has been inserted. |
||
66 | * => 'callback' callable optional, useful if you don't want to issue a separate hook for updating your data, it is called immediately after the data was inserted or deleted and before the actual hook. Uses call_helper(); so the same format for your function/method can be applied here. |
||
67 | * => 'json' boolean optional defaults to false, if true the Like class will return a json object as response instead of HTML. |
||
68 | */ |
||
69 | protected $_validLikes = array( |
||
70 | 'can_like' => false, |
||
71 | 'redirect' => '', |
||
72 | 'type' => '', |
||
73 | 'flush_cache' => '', |
||
74 | 'callback' => false, |
||
75 | 'json' => false, |
||
76 | ); |
||
77 | |||
78 | /** |
||
79 | * @var array The current user info ($user_info). |
||
80 | */ |
||
81 | protected $_user; |
||
82 | |||
83 | /** |
||
84 | * @var integer The topic ID, used for liking messages. |
||
85 | */ |
||
86 | protected $_idTopic = 0; |
||
87 | |||
88 | /** |
||
89 | * @var boolean to know if response(); will be executed as normal. If this is set to false it indicates the method already solved its own way to send back a response. |
||
90 | */ |
||
91 | protected $_setResponse = true; |
||
92 | |||
93 | /** |
||
94 | * Likes::__construct() |
||
95 | * |
||
96 | * Sets the basic data needed for the rest of the process. |
||
97 | */ |
||
98 | public function __construct() |
||
99 | { |
||
100 | global $db_show_debug; |
||
101 | |||
102 | $this->_type = isset($_GET['ltype']) ? $_GET['ltype'] : ''; |
||
103 | $this->_content = isset($_GET['like']) ? (int) $_GET['like'] : 0; |
||
104 | $this->_js = isset($_GET['js']) ? true : false; |
||
105 | $this->_sa = isset($_GET['sa']) ? $_GET['sa'] : 'like'; |
||
|
|||
106 | $this->_extra = isset($_GET['extra']) ? $_GET['extra'] : false; |
||
107 | |||
108 | // We do not want to output debug information here. |
||
109 | if ($this->_js) |
||
110 | $db_show_debug = false; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Likes::call() |
||
115 | * |
||
116 | * The main handler. Verifies permissions (whether the user can see the content in question), dispatch different method for different sub-actions. |
||
117 | * Accessed from index.php?action=likes |
||
118 | */ |
||
119 | public function call() |
||
120 | { |
||
121 | global $context; |
||
122 | |||
123 | $this->_user = $context['user']; |
||
124 | |||
125 | // Make sure the user can see and like your content. |
||
126 | $this->check(); |
||
127 | |||
128 | $subActions = array( |
||
129 | 'like', |
||
130 | 'view', |
||
131 | 'delete', |
||
132 | 'insert', |
||
133 | '_count', |
||
134 | ); |
||
135 | |||
136 | // So at this point, whatever type of like the user supplied and the item of content in question, |
||
137 | // we know it exists, now we need to figure out what we're doing with that. |
||
138 | if (in_array($this->_sa, $subActions) && !is_string($this->_error)) |
||
139 | { |
||
140 | // To avoid ambiguity, turn the property to a normal var. |
||
141 | $call = $this->_sa; |
||
142 | |||
143 | // Guest can only view likes. |
||
144 | if ($call != 'view') |
||
145 | is_not_guest(); |
||
146 | |||
147 | checkSession('get'); |
||
148 | |||
149 | // Call the appropriate method. |
||
150 | $this->$call(); |
||
151 | } |
||
152 | |||
153 | // else An error message. |
||
154 | $this->response(); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Likes::get() |
||
159 | * |
||
160 | * A simple getter for all protected properties. |
||
161 | * Accessed from index.php?action=likes |
||
162 | * |
||
163 | * @param string $property The name of the property to get. |
||
164 | * @return mixed Either return the property or false if there isn't a property with that name. |
||
165 | */ |
||
166 | public function get($property = '') |
||
167 | { |
||
168 | // All properties inside Likes are protected, thus, an underscore is used. |
||
169 | $property = '_' . $property; |
||
170 | return property_exists($this, $property) ? $this->$property : false; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Likes::check() |
||
175 | * |
||
176 | * Performs basic checks on the data provided, checks for a valid msg like. |
||
177 | * Calls integrate_valid_likes hook for retrieving all the data needed and apply checks based on the data provided. |
||
178 | */ |
||
179 | protected function check() |
||
180 | { |
||
181 | global $smcFunc, $modSettings; |
||
182 | |||
183 | // This feature is currently disable. |
||
184 | if (empty($modSettings['enable_likes'])) |
||
185 | return $this->_error = 'like_disable'; |
||
186 | |||
187 | // Zerothly, they did indicate some kind of content to like, right? |
||
188 | preg_match('~^([a-z0-9\-\_]{1,6})~i', $this->_type, $matches); |
||
189 | $this->_type = isset($matches[1]) ? $matches[1] : ''; |
||
190 | |||
191 | if ($this->_type == '' || $this->_content <= 0) |
||
192 | return $this->_error = 'cannot_'; |
||
193 | |||
194 | // First we need to verify if the user can see the type of content or not. This is set up to be extensible, |
||
195 | // so we'll check for the one type we do know about, and if it's not that, we'll defer to any hooks. |
||
196 | if ($this->_type == 'msg') |
||
197 | { |
||
198 | // So we're doing something off a like. We need to verify that it exists, and that the current user can see it. |
||
199 | // Fortunately for messages, this is quite easy to do - and we'll get the topic id while we're at it, because |
||
200 | // we need this later for other things. |
||
201 | $request = $smcFunc['db_query']('', ' |
||
202 | SELECT m.id_topic, m.id_member |
||
203 | FROM {db_prefix}messages AS m |
||
204 | WHERE {query_see_message_board} |
||
205 | AND m.id_msg = {int:msg}', |
||
206 | array( |
||
207 | 'msg' => $this->_content, |
||
208 | ) |
||
209 | ); |
||
210 | if ($smcFunc['db_num_rows']($request) == 1) |
||
211 | list ($this->_idTopic, $topicOwner) = $smcFunc['db_fetch_row']($request); |
||
212 | |||
213 | $smcFunc['db_free_result']($request); |
||
214 | if (empty($this->_idTopic)) |
||
215 | return $this->_error = 'cannot_'; |
||
216 | |||
217 | // So we know what topic it's in and more importantly we know the user can see it. |
||
218 | // If we're not viewing, we need some info set up. |
||
219 | $this->_validLikes['type'] = 'msg'; |
||
220 | $this->_validLikes['flush_cache'] = 'likes_topic_' . $this->_idTopic . '_' . $this->_user['id']; |
||
221 | $this->_validLikes['redirect'] = 'topic=' . $this->_idTopic . '.msg' . $this->_content . '#msg' . $this->_content; |
||
222 | |||
223 | $this->_validLikes['can_like'] = ($this->_user['id'] == $topicOwner ? 'cannot_like_content' : (allowedTo('likes_like') ? true : 'cannot_like_content')); |
||
224 | } |
||
225 | |||
226 | else |
||
227 | { |
||
228 | // Modders: This will give you whatever the user offers up in terms of liking, e.g. $this->_type=msg, $this->_content=1 |
||
229 | // When you hook this, check $this->_type first. If it is not something your mod worries about, return false. |
||
230 | // Otherwise, fill an array according to the docs for $this->_validLikes. Determine (however you need to) that the user can see and can_like the relevant liked content (and it exists) Remember that users can't like their own content. |
||
231 | // If the user can like it, you MUST return your type in the 'type' key back. |
||
232 | // See also issueLike() for further notes. |
||
233 | $can_like = call_integration_hook('integrate_valid_likes', array($this->_type, $this->_content, $this->_sa, $this->_js, $this->_extra)); |
||
234 | |||
235 | $found = false; |
||
236 | if (!empty($can_like)) |
||
237 | { |
||
238 | $can_like = (array) $can_like; |
||
239 | foreach ($can_like as $result) |
||
240 | { |
||
241 | if ($result !== false) |
||
242 | { |
||
243 | // Match the type with what we already have. |
||
244 | if (!isset($result['type']) || $result['type'] != $this->_type) |
||
245 | return $this->_error = 'not_valid_like_type'; |
||
246 | |||
247 | // Fill out the rest. |
||
248 | $this->_type = $result['type']; |
||
249 | $this->_validLikes = array_merge($this->_validLikes, $result); |
||
250 | $found = true; |
||
251 | break; |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 | |||
256 | if (!$found) |
||
257 | return $this->_error = 'cannot_'; |
||
258 | } |
||
259 | |||
260 | // Does the user can like this? Viewing a list of likes doesn't require this permission. |
||
261 | if ($this->_sa != 'view' && isset($this->_validLikes['can_like']) && is_string($this->_validLikes['can_like'])) |
||
262 | return $this->_error = $this->_validLikes['can_like']; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Likes::delete() |
||
267 | * |
||
268 | * Deletes an entry from user_likes table, needs 3 properties: $_content, $_type and $_user['id']. |
||
269 | */ |
||
270 | protected function delete() |
||
271 | { |
||
272 | global $smcFunc; |
||
273 | |||
274 | $smcFunc['db_query']('', ' |
||
275 | DELETE FROM {db_prefix}user_likes |
||
276 | WHERE content_id = {int:like_content} |
||
277 | AND content_type = {string:like_type} |
||
278 | AND id_member = {int:id_member}', |
||
279 | array( |
||
280 | 'like_content' => $this->_content, |
||
281 | 'like_type' => $this->_type, |
||
282 | 'id_member' => $this->_user['id'], |
||
283 | ) |
||
284 | ); |
||
285 | |||
286 | // Are we calling this directly? if so, set a proper data for the response. Do note that __METHOD__ returns both the class name and the function name. |
||
287 | if ($this->_sa == __FUNCTION__) |
||
288 | $this->_data = __FUNCTION__; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Likes::insert() |
||
293 | * |
||
294 | * Inserts a new entry on user_likes table. Creates a background task for the inserted entry. |
||
295 | */ |
||
296 | protected function insert() |
||
297 | { |
||
298 | global $smcFunc; |
||
299 | |||
300 | // Any last minute changes? Temporarily turn the passed properties to normal vars to prevent unexpected behaviour with other methods using these properties. |
||
301 | $type = $this->_type; |
||
302 | $content = $this->_content; |
||
303 | $user = $this->_user; |
||
304 | $time = time(); |
||
305 | |||
306 | call_integration_hook('integrate_issue_like_before', array(&$type, &$content, &$user, &$time)); |
||
307 | |||
308 | // Insert the like. |
||
309 | $smcFunc['db_insert']('insert', |
||
310 | '{db_prefix}user_likes', |
||
311 | array('content_id' => 'int', 'content_type' => 'string-6', 'id_member' => 'int', 'like_time' => 'int'), |
||
312 | array($content, $type, $user['id'], $time), |
||
313 | array('content_id', 'content_type', 'id_member') |
||
314 | ); |
||
315 | |||
316 | // Add a background task to process sending alerts. |
||
317 | // Mod author, you can add your own background task for your own custom like event using the "integrate_issue_like" hook or your callback, both are immediately called after this. |
||
318 | if ($this->_type == 'msg') |
||
319 | $smcFunc['db_insert']('insert', |
||
320 | '{db_prefix}background_tasks', |
||
321 | array('task_file' => 'string', 'task_class' => 'string', 'task_data' => 'string', 'claimed_time' => 'int'), |
||
322 | array('$sourcedir/tasks/Likes-Notify.php', 'Likes_Notify_Background', $smcFunc['json_encode'](array( |
||
323 | 'content_id' => $content, |
||
324 | 'content_type' => $type, |
||
325 | 'sender_id' => $user['id'], |
||
326 | 'sender_name' => $user['name'], |
||
327 | 'time' => $time, |
||
328 | )), 0), |
||
329 | array('id_task') |
||
330 | ); |
||
331 | |||
332 | // Are we calling this directly? if so, set a proper data for the response. Do note that __METHOD__ returns both the class name and the function name. |
||
333 | if ($this->_sa == __FUNCTION__) |
||
334 | $this->_data = __FUNCTION__; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Likes::_count() |
||
339 | * |
||
340 | * Sets $_numLikes with the actual number of likes your content has, needs two properties: $_content and $_view. When called directly it will return the number of likes as response. |
||
341 | */ |
||
342 | protected function _count() |
||
343 | { |
||
344 | global $smcFunc; |
||
345 | |||
346 | $request = $smcFunc['db_query']('', ' |
||
347 | SELECT COUNT(*) |
||
348 | FROM {db_prefix}user_likes |
||
349 | WHERE content_id = {int:like_content} |
||
350 | AND content_type = {string:like_type}', |
||
351 | array( |
||
352 | 'like_content' => $this->_content, |
||
353 | 'like_type' => $this->_type, |
||
354 | ) |
||
355 | ); |
||
356 | list ($this->_numLikes) = $smcFunc['db_fetch_row']($request); |
||
357 | $smcFunc['db_free_result']($request); |
||
358 | |||
359 | // If you want to call this directly, fill out _data property too. |
||
360 | if ($this->_sa == __FUNCTION__) |
||
361 | $this->_data = $this->_numLikes; |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Likes::like() |
||
366 | * |
||
367 | * Performs a like action, either like or unlike. Counts the total of likes and calls a hook after the event. |
||
368 | */ |
||
369 | protected function like() |
||
370 | { |
||
371 | global $smcFunc; |
||
372 | |||
373 | // Safety first! |
||
374 | if (empty($this->_type) || empty($this->_content)) |
||
375 | return $this->_error = 'cannot_'; |
||
376 | |||
377 | // Do we already like this? |
||
378 | $request = $smcFunc['db_query']('', ' |
||
379 | SELECT content_id, content_type, id_member |
||
380 | FROM {db_prefix}user_likes |
||
381 | WHERE content_id = {int:like_content} |
||
382 | AND content_type = {string:like_type} |
||
383 | AND id_member = {int:id_member}', |
||
384 | array( |
||
385 | 'like_content' => $this->_content, |
||
386 | 'like_type' => $this->_type, |
||
387 | 'id_member' => $this->_user['id'], |
||
388 | ) |
||
389 | ); |
||
390 | $this->_alreadyLiked = (bool) $smcFunc['db_num_rows']($request) != 0; |
||
391 | $smcFunc['db_free_result']($request); |
||
392 | |||
393 | if ($this->_alreadyLiked) |
||
394 | $this->delete(); |
||
395 | |||
396 | else |
||
397 | $this->insert(); |
||
398 | |||
399 | // Now, how many people like this content now? We *could* just +1 / -1 the relevant container but that has proven to become unstable. |
||
400 | $this->_count(); |
||
401 | |||
402 | // Update the likes count for messages. |
||
403 | if ($this->_type == 'msg') |
||
404 | $this->msgIssueLike(); |
||
405 | |||
406 | // Any callbacks? |
||
407 | elseif (!empty($this->_validLikes['callback'])) |
||
408 | { |
||
409 | $call = call_helper($this->_validLikes['callback'], true); |
||
410 | |||
411 | if (!empty($call)) |
||
412 | call_user_func_array($call, array($this)); |
||
413 | } |
||
414 | |||
415 | // Sometimes there might be other things that need updating after we do this like. |
||
416 | call_integration_hook('integrate_issue_like', array($this)); |
||
417 | |||
418 | // Now some clean up. This is provided here for any like handlers that want to do any cache flushing. |
||
419 | // This way a like handler doesn't need to explicitly declare anything in integrate_issue_like, but do so |
||
420 | // in integrate_valid_likes where it absolutely has to exist. |
||
421 | if (!empty($this->_validLikes['flush_cache'])) |
||
422 | cache_put_data($this->_validLikes['flush_cache'], null); |
||
423 | |||
424 | // All done, start building the data to pass as response. |
||
425 | $this->_data = array( |
||
426 | 'id_topic' => !empty($this->_idTopic) ? $this->_idTopic : 0, |
||
427 | 'id_content' => $this->_content, |
||
428 | 'count' => $this->_numLikes, |
||
429 | 'can_like' => $this->_validLikes['can_like'], |
||
430 | 'already_liked' => empty($this->_alreadyLiked), |
||
431 | 'type' => $this->_type, |
||
432 | ); |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * Likes::msgIssueLike() |
||
437 | * |
||
438 | * Partly it indicates how it's supposed to work and partly it deals with updating the count of likes |
||
439 | * attached to this message now. |
||
440 | */ |
||
441 | function msgIssueLike() |
||
455 | ) |
||
456 | ); |
||
457 | |||
458 | // Note that we could just as easily have cleared the cache here, or set up the redirection address |
||
459 | // but if your liked content doesn't need to do anything other than have the record in smf_user_likes, |
||
460 | // there's no point in creating another function unnecessarily. |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * Likes::view() |
||
465 | * |
||
466 | * This is for viewing the people who liked a thing. |
||
467 | * Accessed from index.php?action=likes;view and should generally load in a popup. |
||
468 | * We use a template for this in case themers want to style it. |
||
469 | */ |
||
470 | function view() |
||
525 | } |
||
526 | |||
527 | /** |
||
528 | * Likes::response() |
||
529 | * |
||
530 | * Checks if the user can use JavaScript and acts accordingly. |
||
531 | * Calls the appropriate sub-template for each method |
||
532 | * Handles error messages. |
||
533 | */ |
||
534 | protected function response() |
||
591 | } |
||
592 | } |
||
593 | } |
||
594 | |||
595 | /** |
||
596 | * Outputs a JSON-encoded response |
||
597 | */ |
||
598 | protected function jsonResponse() |
||
621 | } |
||
622 | } |
||
623 | |||
682 | ?> |