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

BuddyModel::setRow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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