Completed
Push — work-fleets ( 73ed38...ce5b3a )
by SuperNova.WS
05:53
created

Buddy   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 173
rs 10
c 1
b 0
f 0
wmc 28
lcom 2
cbo 6

8 Methods

Rating   Name   Duplication   Size   Complexity  
A db_buddy_update_status() 0 7 1
A db_buddy_delete() 0 5 1
A db_buddy_check_relation() 0 9 1
A db_buddy_insert() 0 3 1
A db_buddy_list_by_user() 0 21 2
C accept() 0 31 7
C delete() 0 27 7
C beFriend() 0 30 8
1
<?php
2
3
class Buddy extends Entity {
4
5
  public static $tableName = 'buddy';
6
  public static $idField = 'BUDDY_ID';
7
8
9
  // TODO - remove public static function db_buddy_update_status($buddy_id, $status) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
41% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
10
  public function db_buddy_update_status($status) {
11
    $buddy_id = idval($this->row['BUDDY_ID']);
12
13
    doquery("UPDATE `{{buddy}}` SET `BUDDY_STATUS` = {$status} WHERE `BUDDY_ID` = '{$buddy_id}' LIMIT 1;");
14
15
    return classSupernova::$db->db_affected_rows();
16
  }
17
18
  public function db_buddy_delete($buddy_id) {
0 ignored issues
show
Unused Code introduced by
The parameter $buddy_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
19
    $buddy_id = idval($this->row['BUDDY_ID']);
20
21
    doquery("DELETE FROM `{{buddy}}` WHERE `BUDDY_ID` = '{$buddy_id}' LIMIT 1;");
22
  }
23
24
  public function db_buddy_check_relation($user, $new_friend_row) {
25
    return static::$dbStatic->doQueryFetch(
26
      "SELECT `BUDDY_ID` FROM `{{buddy}}` WHERE
27
      (`BUDDY_SENDER_ID` = {$user['id']} AND `BUDDY_OWNER_ID` = {$new_friend_row['id']})
28
      OR
29
      (`BUDDY_SENDER_ID` = {$new_friend_row['id']} AND `BUDDY_OWNER_ID` = {$user['id']})
30
      LIMIT 1 FOR UPDATE;"
31
    );
32
  }
33
34
  public function db_buddy_insert($userId_safe, $new_friend_row_id_safe, $new_request_text) {
35
    doquery("INSERT INTO `{{buddy}}` SET `BUDDY_SENDER_ID` = {$userId_safe}, `BUDDY_OWNER_ID` = '{$new_friend_row_id_safe}', `BUDDY_REQUEST` = '{$new_request_text}';");
36
  }
37
38
  /**
39
   * @param db_mysql $db
40
   * @param mixed    $user_id
41
   *
42
   * @return DbEmptyIterator
43
   */
44
  public static function db_buddy_list_by_user($db, $user_id) {
45
    return ($user_id = idval($user_id)) ? $db->doQueryIterator(
0 ignored issues
show
Bug Compatibility introduced by
The expression ($user_id = idval($user_...new \DbEmptyIterator(); of type DbMysqliResultIterator|DbEmptyIterator adds the type DbMysqliResultIterator to the return on line 45 which is incompatible with the return type documented by Buddy::db_buddy_list_by_user of type DbEmptyIterator.
Loading history...
46
      "SELECT
47
      b.*,
48
      IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID) AS BUDDY_USER_ID,
49
      u.username AS BUDDY_USER_NAME,
50
      p.name AS BUDDY_PLANET_NAME,
51
      p.galaxy AS BUDDY_PLANET_GALAXY,
52
      p.system AS BUDDY_PLANET_SYSTEM,
53
      p.planet AS BUDDY_PLANET_PLANET,
54
      a.id AS BUDDY_ALLY_ID,
55
      a.ally_name AS BUDDY_ALLY_NAME,
56
      u.onlinetime
57
    FROM {{buddy}} AS b
58
      LEFT JOIN {{users}} AS u ON u.id = IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID)
59
      LEFT JOIN {{planets}} AS p ON p.id_owner = u.id AND p.id = id_planet
60
      LEFT JOIN {{alliance}} AS a ON a.id = u.ally_id
61
    WHERE (`BUDDY_OWNER_ID` = {$user_id}) OR `BUDDY_SENDER_ID` = {$user_id}
62
    ORDER BY BUDDY_STATUS, BUDDY_ID"
63
    ) : new DbEmptyIterator();
64
  }
65
66
  /**
67
   * @param array $user
68
   *
69
   * @throws Exception
70
   */
71
  public function accept($user) {
72
    $buddy_row = $this->row;
73
74
    if ($buddy_row['BUDDY_SENDER_ID'] == $user['id']) {
75
      throw new Exception('buddy_err_accept_own', ERR_ERROR);
76
    }
77
78
    if ($buddy_row['BUDDY_OWNER_ID'] != $user['id']) {
79
      throw new Exception('buddy_err_accept_alien', ERR_ERROR);
80
    }
81
82
    if ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_ACTIVE) {
83
      throw new Exception('buddy_err_accept_already', ERR_WARNING);
84
    }
85
86
    if ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_DENIED) {
87
      throw new Exception('buddy_err_accept_denied', ERR_ERROR);
88
    }
89
90
    if ($buddy_row['BUDDY_STATUS'] != BUDDY_REQUEST_WAITING) {
91
      throw new Exception('buddy_err_unknown_status', ERR_ERROR);
92
    }
93
94
    $result = $this->db_buddy_update_status(BUDDY_REQUEST_ACTIVE);
95
    if ($result) {
96
      DBStaticMessages::msgSendFromPlayerBuddy($buddy_row['BUDDY_SENDER_ID'], $user, 'buddy_msg_accept_title', 'buddy_msg_accept_text');
97
      throw new Exception('buddy_err_accept_none', ERR_NONE);
98
    } else {
99
      throw new Exception('buddy_err_accept_internal', ERR_ERROR);
100
    }
101
  }
102
103
  /**
104
   * @param array $user
105
   *
106
   * @throws Exception
107
   */
108
  public function delete($user) {
109
    $buddy_row = $this->row;
110
111
    if ($buddy_row['BUDDY_SENDER_ID'] != $user['id'] && $buddy_row['BUDDY_OWNER_ID'] != $user['id']) {
112
      throw new Exception('buddy_err_delete_alien', ERR_ERROR);
113
    }
114
115
    if ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_ACTIVE) {
116
      // Existing friendship
117
      $ex_friend_id = $buddy_row['BUDDY_SENDER_ID'] == $user['id'] ? $buddy_row['BUDDY_OWNER_ID'] : $buddy_row['BUDDY_SENDER_ID'];
118
119
      DBStaticMessages::msgSendFromPlayerBuddy($ex_friend_id, $user, 'buddy_msg_unfriend_title', 'buddy_msg_unfriend_text');
120
121
      $this->db_buddy_delete($buddy_row['BUDDY_ID']);
122
      throw new Exception('buddy_err_unfriend_none', ERR_NONE);
123
    } elseif ($buddy_row['BUDDY_SENDER_ID'] == $user['id']) {
124
      // Player's outcoming request - either denied or waiting
125
      $this->db_buddy_delete($buddy_row['BUDDY_ID']);
126
      throw new Exception('buddy_err_delete_own', ERR_NONE);
127
    } elseif ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_WAITING) {
128
      // Deny incoming request
129
      DBStaticMessages::msgSendFromPlayerBuddy($buddy_row['BUDDY_SENDER_ID'], $user, 'buddy_msg_deny_title', 'buddy_msg_deny_text');
130
131
      $this->db_buddy_update_status(BUDDY_REQUEST_DENIED);
132
      throw new Exception('buddy_err_deny_none', ERR_NONE);
133
    }
134
  }
135
136
  /**
137
   * @param array  $user
138
   * @param mixed  $new_friend_id
139
   * @param string $new_friend_name_unsafe
140
   * @param string $new_request_text_safe
141
   *
142
   * @throws Exception
143
   */
144
  public function beFriend($user, $new_friend_id, $new_friend_name_unsafe, $new_request_text_safe) {
145
    $new_friend_row = array();
146
    if ($new_friend_id) {
147
      $new_friend_row = DBStaticUser::db_user_by_id($new_friend_id, true, '`id`, `username`');
148
    } elseif ($new_friend_name_unsafe) {
149
      $new_friend_row = DBStaticUser::db_user_by_username($new_friend_name_unsafe, true, '`id`, `username`');
150
    }
151
152
    if (empty($new_friend_row) || empty($new_friend_row['id'])) {
153
      return;
154
    }
155
156
    if ($new_friend_row['id'] == $user['id']) {
157
      unset($new_friend_row);
158
      throw new Exception('buddy_err_adding_self', ERR_ERROR);
159
    }
160
161
    // Checking for user name & request text - in case if it was request to adding new request
162
    if ($new_request_text_safe) {
163
      $check_relation = $this->db_buddy_check_relation($user, $new_friend_row);
164
      if (isset($check_relation['BUDDY_ID'])) {
165
        throw new Exception('buddy_err_adding_exists', ERR_WARNING);
166
      }
167
168
      DBStaticMessages::msgSendFromPlayerBuddy($new_friend_row['id'], $user, 'buddy_msg_adding_title', 'buddy_msg_adding_text');
169
170
      $this->db_buddy_insert(idval($user['id']), idval($new_friend_row['id']), $new_request_text_safe);
171
      throw new Exception('buddy_err_adding_none', ERR_NONE);
172
    }
173
  }
174
175
}
176