Completed
Push — work-fleets ( 7496c9...2bd11a )
by SuperNova.WS
06:07
created

BuddyView::makeTemplate()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 64
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 44
nc 24
nop 2
dl 0
loc 64
rs 8.6346
c 2
b 0
f 0
ccs 0
cts 49
cp 0
crap 42

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
 * Created by Gorlum 30.07.2016 20:52
4
 */
5
namespace Buddy;
6
use Common\GlobalContainer;
7
8
9
class BuddyView {
10
11
  /**
12
   * @param GlobalContainer $gc
13
   * @param array           $user
14
   *
15
   * @return \template
16
   */
17
  public function makeTemplate($gc, $user) {
18
    $playerLocale = $gc->localePlayer;
19
20
    $cBuddy = new BuddyRoutingParams();
21
22
    $cBuddy->gc = $gc;
23
    $cBuddy->buddy_id = sys_get_param_id('buddy_id');
24
    $cBuddy->mode = sys_get_param_str('mode');
25
    $cBuddy->newFriendIdSafe = sys_get_param_id('request_user_id');
26
    $cBuddy->new_friend_name_unsafe = sys_get_param_str_unsafe('request_user_name');
27
    $cBuddy->new_request_text_unsafe = sys_get_param_str_unsafe('request_text');
28
    $cBuddy->playerArray = $user;
29
30
    $cBuddy->playerId = function (BuddyRoutingParams $cBuddy) {
31
      return $cBuddy->playerArray['id'];
32
    };
33
    $cBuddy->playerName = function (BuddyRoutingParams $cBuddy) {
34
      return $cBuddy->playerArray['username'];
35
    };
36
    $cBuddy->playerNameAndCoordinates = function (BuddyRoutingParams $cBuddy) {
37
      return "{$cBuddy->playerArray['username']} " . uni_render_coordinates($cBuddy->playerArray);
38
    };
39
40
    $result = array();
41
    sn_db_transaction_start();
42
    try {
43
      $gc->buddy->route($cBuddy);
0 ignored issues
show
Bug introduced by
The method route cannot be called on $gc->buddy (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
44
    } catch (BuddyException $e) {
45
      $exceptionCode = \ResultMessages::parseException($e, $result);
46
47
      $exceptionCode == ERR_NONE ? sn_db_transaction_commit() : sn_db_transaction_rollback();
48
    }
49
    sn_db_transaction_rollback();
50
    unset($buddy);
51
52
    empty($template_result) ? $template_result = array() : false;
0 ignored issues
show
Bug introduced by
The variable $template_result seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
53
54
    foreach (BuddyModel::db_buddy_list_by_user($gc->db, $user['id']) as $row) {
55
      $row['BUDDY_REQUEST'] = sys_bbcodeParse($row['BUDDY_REQUEST']);
56
57
      $row['BUDDY_ACTIVE'] = $row['BUDDY_STATUS'] == BUDDY_REQUEST_ACTIVE;
58
      $row['BUDDY_DENIED'] = $row['BUDDY_STATUS'] == BUDDY_REQUEST_DENIED;
59
      $row['BUDDY_INCOMING'] = $row['BUDDY_OWNER_ID'] == $user['id'];
60
      $row['BUDDY_ONLINE'] = floor((SN_TIME_NOW - $row['onlinetime']) / 60);
61
62
      $template_result['.']['buddy'][] = $row;
63
    }
64
65
    $template_result += array(
66
      'PAGE_HEADER'       => $playerLocale['buddy_buddies'],
67
      'PAGE_HINT'         => $playerLocale['buddy_hint'],
68
      'USER_ID'           => $user['id'],
69
      'REQUEST_USER_ID'   => $cBuddy->newFriendIdSafe,
70
      'REQUEST_USER_NAME' => $cBuddy->new_friend_name_unsafe,
71
    );
72
73
    $template_result['.']['result'] = is_array($template_result['.']['result']) ? $template_result['.']['result'] : array();
74
    $template_result['.']['result'] += $result;
75
76
    $template = gettemplate('buddy', true);
77
    $template->assign_recursive($template_result);
78
79
    return $template;
80
  }
81
82
}
83