Completed
Push — work-fleets ( b19a67...de8005 )
by SuperNova.WS
06:26
created

BuddyModel::db_buddy_list_by_user()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 7

Duplication

Lines 21
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 21
loc 21
rs 9.3142
c 1
b 0
f 0
1
<?php
2
3
namespace Buddy;
4
5
use classSupernova;
6
use db_mysql;
7
use DbEmptyIterator;
8
use DbMysqliResultIterator;
9
use DBStaticMessages;
10
use DBStaticUser;
11
use Entity;
12
use Pimple\GlobalContainer;
13
14
/**
15
 * Class BuddyModel
16
 *
17
 * @property int|float $dbId Buddy record DB ID
18
 * @property int|float $playerSenderId Who makes buddy request
19
 * @property int|float $playerOwnerId To whom this buddy request made
20
 * @property int       $buddyStatusId Current buddy request status
21
 * @property string    $requestText Request text
22
 *
23
 * @package Buddy
24
 */
25
class BuddyModel extends Entity {
26
27
  public static $tableName = 'buddy';
28
  public static $idField = 'BUDDY_ID';
29
30
  /**
31
   * @var BuddyRow $_container
32
   */
33
  public $_container;
34
  public static $_containerName = 'Buddy\BuddyRow';
35
36
  // TODO - make it work with Model's properties
37
  /**
38
   * Property list
39
   *
40
   * @var array
41
   */
42
  protected static $_properties = array(
43
    'dbId'           => true,
44
    'playerSenderId' => true,
45
    'playerOwnerId'  => true,
46
    'buddyStatusId'  => true,
47
    'requestText'    => true,
48
  );
49
50
51
  // 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...
52
  public function db_buddy_update_status($status) {
53
    $buddy_id = idval($this->dbId);
54
55
    doquery("UPDATE `{{buddy}}` SET `BUDDY_STATUS` = {$status} WHERE `BUDDY_ID` = '{$buddy_id}' LIMIT 1;");
56
57
    return classSupernova::$db->db_affected_rows();
58
  }
59
60 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...
61
    return static::$dbStatic->doQueryFetch(
62
      "SELECT `BUDDY_ID` FROM `{{buddy}}` WHERE
63
      (`BUDDY_SENDER_ID` = {$user['id']} AND `BUDDY_OWNER_ID` = {$new_friend_row['id']})
64
      OR
65
      (`BUDDY_SENDER_ID` = {$new_friend_row['id']} AND `BUDDY_OWNER_ID` = {$user['id']})
66
      LIMIT 1 FOR UPDATE;"
67
    );
68
  }
69
70
  /**
71
   * @param db_mysql $db
72
   * @param mixed    $user_id
73
   *
74
   * @return DbEmptyIterator|DbMysqliResultIterator
75
   */
76 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...
77
    return ($user_id = idval($user_id)) ? $db->doQueryIterator(
78
      "SELECT
79
      b.*,
80
      IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID) AS BUDDY_USER_ID,
81
      u.username AS BUDDY_USER_NAME,
82
      p.name AS BUDDY_PLANET_NAME,
83
      p.galaxy AS BUDDY_PLANET_GALAXY,
84
      p.system AS BUDDY_PLANET_SYSTEM,
85
      p.planet AS BUDDY_PLANET_PLANET,
86
      a.id AS BUDDY_ALLY_ID,
87
      a.ally_name AS BUDDY_ALLY_NAME,
88
      u.onlinetime
89
    FROM {{buddy}} AS b
90
      LEFT JOIN {{users}} AS u ON u.id = IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID)
91
      LEFT JOIN {{planets}} AS p ON p.id_owner = u.id AND p.id = id_planet
92
      LEFT JOIN {{alliance}} AS a ON a.id = u.ally_id
93
    WHERE (`BUDDY_OWNER_ID` = {$user_id}) OR `BUDDY_SENDER_ID` = {$user_id}
94
    ORDER BY BUDDY_STATUS, BUDDY_ID"
95
    ) : new DbEmptyIterator();
96
  }
97
98
  /**
99
   * @param array $user
100
   *
101
   * @throws BuddyException
102
   */
103
  public function accept($user) {
104
    if ($this->playerSenderId == $user['id']) {
105
      throw new BuddyException('buddy_err_accept_own', ERR_ERROR);
106
    }
107
108
    if ($this->playerOwnerId != $user['id']) {
109
      throw new BuddyException('buddy_err_accept_alien', ERR_ERROR);
110
    }
111
112
    if ($this->buddyStatusId == BUDDY_REQUEST_ACTIVE) {
113
      throw new BuddyException('buddy_err_accept_already', ERR_WARNING);
114
    }
115
116
    if ($this->buddyStatusId == BUDDY_REQUEST_DENIED) {
117
      throw new BuddyException('buddy_err_accept_denied', ERR_ERROR);
118
    }
119
120
    if ($this->buddyStatusId != BUDDY_REQUEST_WAITING) {
121
      throw new BuddyException('buddy_err_unknown_status', ERR_ERROR);
122
    }
123
124
    $result = $this->db_buddy_update_status(BUDDY_REQUEST_ACTIVE);
125 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...
126
      DBStaticMessages::msgSendFromPlayerBuddy($this->playerSenderId, $user, 'buddy_msg_accept_title', 'buddy_msg_accept_text');
127
      throw new BuddyException('buddy_err_accept_none', ERR_NONE);
128
    } else {
129
      throw new BuddyException('buddy_err_accept_internal', ERR_ERROR);
130
    }
131
  }
132
133
  /**
134
   * @param array $user
135
   *
136
   * @throws BuddyException
137
   */
138
  public function decline($user) {
139
    if ($this->playerSenderId != $user['id'] && $this->playerOwnerId != $user['id']) {
140
      throw new BuddyException('buddy_err_delete_alien', ERR_ERROR);
141
    }
142
143
    if ($this->buddyStatusId == BUDDY_REQUEST_ACTIVE) {
144
      // Existing friendship
145
      $ex_friend_id = $this->playerSenderId == $user['id'] ? $this->playerOwnerId : $this->playerSenderId;
146
147
      DBStaticMessages::msgSendFromPlayerBuddy($ex_friend_id, $user, 'buddy_msg_unfriend_title', 'buddy_msg_unfriend_text');
148
149
      $this->delete();
150
      throw new BuddyException('buddy_err_unfriend_none', ERR_NONE);
151
    } elseif ($this->playerSenderId == $user['id']) {
152
      // Player's outcoming request - either denied or waiting
153
      $this->delete();
154
      throw new BuddyException('buddy_err_delete_own', ERR_NONE);
155
    } elseif ($this->buddyStatusId == BUDDY_REQUEST_WAITING) {
156
      // Deny incoming request
157
      DBStaticMessages::msgSendFromPlayerBuddy($this->playerSenderId, $user, 'buddy_msg_deny_title', 'buddy_msg_deny_text');
158
159
      $this->db_buddy_update_status(BUDDY_REQUEST_DENIED);
160
      throw new BuddyException('buddy_err_deny_none', ERR_NONE);
161
    }
162
  }
163
164
  /**
165
   * @param array              $user
166
   * @param mixed              $new_friend_id
167
   * @param string             $new_friend_name_unsafe
168
   * @param string             $new_request_text_safe
169
   * @param BuddyRoutingParams $cBuddy
170
   *
171
   * @throws BuddyException
172
   */
173
  public function beFriend($user, $new_friend_id, $new_friend_name_unsafe, $new_request_text_safe, $cBuddy) {
0 ignored issues
show
Unused Code introduced by
The parameter $new_friend_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...
Unused Code introduced by
The parameter $new_friend_name_unsafe 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...
174
    $new_friend_row = array();
175
    if ($cBuddy->new_friend_id_safe) {
176
      $new_friend_row = DBStaticUser::db_user_by_id($cBuddy->new_friend_id_safe, true, '`id`, `username`');
177
    } elseif ($cBuddy->new_friend_name_unsafe) {
178
      $new_friend_row = DBStaticUser::db_user_by_username($cBuddy->new_friend_name_unsafe, true, '`id`, `username`');
179
    }
180
181
    if (empty($new_friend_row) || empty($new_friend_row['id'])) {
182
      return;
183
    }
184
185 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...
186
      unset($new_friend_row);
187
      throw new BuddyException('buddy_err_adding_self', ERR_ERROR);
188
    }
189
190
    // Checking for user name & request text - in case if it was request to adding new request
191
    if ($new_request_text_safe) {
192
      $check_relation = $this->db_buddy_check_relation($user, $new_friend_row);
193
      if (isset($check_relation['BUDDY_ID'])) {
194
        throw new BuddyException('buddy_err_adding_exists', ERR_WARNING);
195
      }
196
197
      DBStaticMessages::msgSendFromPlayerBuddy($new_friend_row['id'], $user, 'buddy_msg_adding_title', 'buddy_msg_adding_text');
198
199
      $this->playerSenderId = idval($user['id']);
200
      $this->playerOwnerId = idval($new_friend_row['id']);
201
      $this->buddyStatusId = BUDDY_REQUEST_WAITING;
202
      $this->requestText = $new_request_text_safe;
203
204
      $this->insert();
205
      throw new BuddyException('buddy_err_adding_none', ERR_NONE);
206
    }
207
  }
208
209
  public function isEmpty() {
210
    return
211
      $this->buddyStatusId == BUDDY_REQUEST_NOT_SET
212
      ||
213
      empty($this->playerSenderId)
214
      ||
215
      empty($this->playerOwnerId);
216
  }
217
218
  /**
219
   * @param array $row
220
   */
221
  // TODO -    PROOF OF CONCEPTION
222
  public function setRow($row) {
223
//    foreach($this->_container->getProperties() as $propertyName => $cork) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% 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...
224
//    }
225
//    $this->_container->dbId = $row['BUDDY_ID'];
226
    $this->dbId = $row[static::$idField];
227
    $this->playerSenderId = $row['BUDDY_SENDER_ID'];
228
    $this->playerOwnerId = $row['BUDDY_OWNER_ID'];
229
    $this->buddyStatusId = $row['BUDDY_STATUS'];
230
    $this->requestText = $row['BUDDY_REQUEST'];
231
  }
232
233
  /**
234
   * Compiles object data into db row
235
   *
236
   * @return array
237
   */
238
  public function getRow($withDbId = true) {
239
    $row = array(
240
      static::$idField  => $this->dbId,
241
      'BUDDY_SENDER_ID' => $this->playerSenderId,
242
      'BUDDY_OWNER_ID'  => $this->playerOwnerId,
243
      'BUDDY_STATUS'    => $this->buddyStatusId,
244
      'BUDDY_REQUEST'   => $this->requestText,
245
    );
246
247
    if (!$withDbId) {
248
      unset($row[static::$idField]);
249
    }
250
251
    return $row;
252
  }
253
254
  public function setDbId($value) {
255
    $this->dbId = $value;
256
  }
257
258
259
  /**
260
   * @return int|float|string
261
   */
262
  public function getDbId() {
263
    return $this->dbId;
264
  }
265
266
  /**
267
   * @param BuddyRoutingParams $cBuddy
268
   *
269
   * @throws BuddyException
270
   */
271
  public function route($cBuddy) {
272
    if ($cBuddy->buddy_id) {
273
      $this->load($cBuddy->buddy_id);
274
275
      if ($this->isEmpty()) {
276
        throw new BuddyException('buddy_err_not_exist', ERR_ERROR);
277
      }
278
279
      switch ($cBuddy->mode) {
280
        case 'accept':
281
          $this->accept($cBuddy->user);
282
        break;
283
        case 'delete':
284
          $this->decline($cBuddy->user);
285
        break;
286
      }
287
    } else {
288
      // New request?
289
      // Checking for user ID - in case if it was request from outside buddy system
290
      if (!empty($cBuddy->new_friend_id_safe) || !empty($cBuddy->new_friend_name_unsafe)) {
291
        $this->beFriend($cBuddy->user, $cBuddy->new_friend_id_safe, $cBuddy->new_friend_name_unsafe, $cBuddy->new_request_text, $cBuddy);
292
      }
293
    }
294
  }
295
296
}
297