Completed
Push — work-fleets ( dea33d...b19a67 )
by SuperNova.WS
05:56
created

Buddy::isEmpty()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 2
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 3
rs 9.2
1
<?php
2
3
class Buddy extends Entity {
4
5
  public static $tableName = 'buddy';
6
  public static $idField = 'BUDDY_ID';
7
8
  /**
9
   * Who makes buddy request
10
   *
11
   * @var int|float|string $playerSenderId
12
   */
13
  public $playerSenderId = 0;
14
  /**
15
   * To whom this buddy request made
16
   *
17
   * @var int|float|string $playerOwnerId
18
   */
19
  public $playerOwnerId = 0;
20
  /**
21
   * Current buddy request status
22
   *
23
   * @var int $buddyStatusId
24
   */
25
  public $buddyStatusId = BUDDY_REQUEST_NOT_SET;
26
  /**
27
   * Request text
28
   *
29
   * @var string $requestText
30
   */
31
  public $requestText = '';
32
33
  // 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...
34 View Code Duplication
  public function db_buddy_update_status($status) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    $buddy_id = idval($this->row['BUDDY_ID']);
36
37
    doquery("UPDATE `{{buddy}}` SET `BUDDY_STATUS` = {$status} WHERE `BUDDY_ID` = '{$buddy_id}' LIMIT 1;");
38
39
    return classSupernova::$db->db_affected_rows();
40
  }
41
42 View Code Duplication
  public function db_buddy_check_relation($user, $new_friend_row) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    return static::$dbStatic->doQueryFetch(
44
      "SELECT `BUDDY_ID` FROM `{{buddy}}` WHERE
45
      (`BUDDY_SENDER_ID` = {$user['id']} AND `BUDDY_OWNER_ID` = {$new_friend_row['id']})
46
      OR
47
      (`BUDDY_SENDER_ID` = {$new_friend_row['id']} AND `BUDDY_OWNER_ID` = {$user['id']})
48
      LIMIT 1 FOR UPDATE;"
49
    );
50
  }
51
52
  /**
53
   * @param db_mysql $db
54
   * @param mixed    $user_id
55
   *
56
   * @return DbEmptyIterator|DbMysqliResultIterator
57
   */
58 View Code Duplication
  public static function db_buddy_list_by_user($db, $user_id) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    return ($user_id = idval($user_id)) ? $db->doQueryIterator(
60
      "SELECT
61
      b.*,
62
      IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID) AS BUDDY_USER_ID,
63
      u.username AS BUDDY_USER_NAME,
64
      p.name AS BUDDY_PLANET_NAME,
65
      p.galaxy AS BUDDY_PLANET_GALAXY,
66
      p.system AS BUDDY_PLANET_SYSTEM,
67
      p.planet AS BUDDY_PLANET_PLANET,
68
      a.id AS BUDDY_ALLY_ID,
69
      a.ally_name AS BUDDY_ALLY_NAME,
70
      u.onlinetime
71
    FROM {{buddy}} AS b
72
      LEFT JOIN {{users}} AS u ON u.id = IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID)
73
      LEFT JOIN {{planets}} AS p ON p.id_owner = u.id AND p.id = id_planet
74
      LEFT JOIN {{alliance}} AS a ON a.id = u.ally_id
75
    WHERE (`BUDDY_OWNER_ID` = {$user_id}) OR `BUDDY_SENDER_ID` = {$user_id}
76
    ORDER BY BUDDY_STATUS, BUDDY_ID"
77
    ) : new DbEmptyIterator();
78
  }
79
80
  /**
81
   * @param array $user
82
   *
83
   * @throws Exception
84
   */
85
  public function accept($user) {
86
    $buddy_row = $this->row;
87
88
    if ($buddy_row['BUDDY_SENDER_ID'] == $user['id']) {
89
      throw new Exception('buddy_err_accept_own', ERR_ERROR);
90
    }
91
92
    if ($buddy_row['BUDDY_OWNER_ID'] != $user['id']) {
93
      throw new Exception('buddy_err_accept_alien', ERR_ERROR);
94
    }
95
96
    if ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_ACTIVE) {
97
      throw new Exception('buddy_err_accept_already', ERR_WARNING);
98
    }
99
100
    if ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_DENIED) {
101
      throw new Exception('buddy_err_accept_denied', ERR_ERROR);
102
    }
103
104
    if ($buddy_row['BUDDY_STATUS'] != BUDDY_REQUEST_WAITING) {
105
      throw new Exception('buddy_err_unknown_status', ERR_ERROR);
106
    }
107
108
    $result = $this->db_buddy_update_status(BUDDY_REQUEST_ACTIVE);
109 View Code Duplication
    if ($result) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
      DBStaticMessages::msgSendFromPlayerBuddy($buddy_row['BUDDY_SENDER_ID'], $user, 'buddy_msg_accept_title', 'buddy_msg_accept_text');
111
      throw new Exception('buddy_err_accept_none', ERR_NONE);
112
    } else {
113
      throw new Exception('buddy_err_accept_internal', ERR_ERROR);
114
    }
115
  }
116
117
  /**
118
   * @param array $user
119
   *
120
   * @throws Exception
121
   */
122
  public function decline($user) {
123
    $buddy_row = $this->row;
124
125
    if ($buddy_row['BUDDY_SENDER_ID'] != $user['id'] && $buddy_row['BUDDY_OWNER_ID'] != $user['id']) {
126
      throw new Exception('buddy_err_delete_alien', ERR_ERROR);
127
    }
128
129
    if ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_ACTIVE) {
130
      // Existing friendship
131
      $ex_friend_id = $buddy_row['BUDDY_SENDER_ID'] == $user['id'] ? $buddy_row['BUDDY_OWNER_ID'] : $buddy_row['BUDDY_SENDER_ID'];
132
133
      DBStaticMessages::msgSendFromPlayerBuddy($ex_friend_id, $user, 'buddy_msg_unfriend_title', 'buddy_msg_unfriend_text');
134
135
      $this->delete();
136
      throw new Exception('buddy_err_unfriend_none', ERR_NONE);
137
    } elseif ($buddy_row['BUDDY_SENDER_ID'] == $user['id']) {
138
      // Player's outcoming request - either denied or waiting
139
      $this->delete();
140
      throw new Exception('buddy_err_delete_own', ERR_NONE);
141
    } elseif ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_WAITING) {
142
      // Deny incoming request
143
      DBStaticMessages::msgSendFromPlayerBuddy($buddy_row['BUDDY_SENDER_ID'], $user, 'buddy_msg_deny_title', 'buddy_msg_deny_text');
144
145
      $this->db_buddy_update_status(BUDDY_REQUEST_DENIED);
146
      throw new Exception('buddy_err_deny_none', ERR_NONE);
147
    }
148
  }
149
150
  /**
151
   * @param array  $user
152
   * @param mixed  $new_friend_id
153
   * @param string $new_friend_name_unsafe
154
   * @param string $new_request_text_safe
155
   *
156
   * @throws Exception
157
   */
158
  public function beFriend($user, $new_friend_id, $new_friend_name_unsafe, $new_request_text_safe) {
159
    $new_friend_row = array();
160 View Code Duplication
    if ($new_friend_id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
      $new_friend_row = DBStaticUser::db_user_by_id($new_friend_id, true, '`id`, `username`');
162
    } elseif ($new_friend_name_unsafe) {
163
      $new_friend_row = DBStaticUser::db_user_by_username($new_friend_name_unsafe, true, '`id`, `username`');
164
    }
165
166
    if (empty($new_friend_row) || empty($new_friend_row['id'])) {
167
      return;
168
    }
169
170 View Code Duplication
    if ($new_friend_row['id'] == $user['id']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
      unset($new_friend_row);
172
      throw new Exception('buddy_err_adding_self', ERR_ERROR);
173
    }
174
175
    // Checking for user name & request text - in case if it was request to adding new request
176
    if ($new_request_text_safe) {
177
      $check_relation = $this->db_buddy_check_relation($user, $new_friend_row);
178
      if (isset($check_relation['BUDDY_ID'])) {
179
        throw new Exception('buddy_err_adding_exists', ERR_WARNING);
180
      }
181
182
      DBStaticMessages::msgSendFromPlayerBuddy($new_friend_row['id'], $user, 'buddy_msg_adding_title', 'buddy_msg_adding_text');
183
184
      $this->row['BUDDY_SENDER_ID'] = idval($user['id']);
185
      $this->row['BUDDY_OWNER_ID'] = idval($new_friend_row['id']);
186
      $this->row['BUDDY_STATUS'] = BUDDY_REQUEST_WAITING;
187
      $this->row['BUDDY_REQUEST'] = $new_request_text_safe;
188
189
      $this->insert();
190
      throw new Exception('buddy_err_adding_none', ERR_NONE);
191
    }
192
  }
193
194
  public function isEmpty() {
195
    return parent::isEmpty() || $this->row['BUDDY_STATUS'] == BUDDY_REQUEST_NOT_SET || empty($this->row['BUDDY_SENDER_ID']) || empty($this->row['BUDDY_OWNER_ID']);
196
  }
197
198
}
199