Completed
Push — work-fleets ( 4ec5b3...fe2ede )
by SuperNova.WS
10:06
created

BuddyModel::db_buddy_update_status()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 8
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 11
rs 9.4285
c 8
b 0
f 0
ccs 0
cts 11
cp 0
crap 2
1
<?php
2
3
namespace Buddy;
4
5
use DbEmptyIterator;
6
use DbMysqliResultIterator;
7
use DBStatic\DBStaticMessages;
8
use DBStatic\DBStaticUser;
9
10
/**
11
 * Class BuddyModel
12
 *
13
 * @method BuddyContainer buildContainer()
14
 * @method BuddyContainer loadById(int|string $dbId)
15
 *
16
 * property int|float|string $playerSenderId Who makes buddy request
17
 * property int|float|string $playerOwnerId To whom this buddy request made
18
 * property int              $buddyStatusId Current buddy request status
19
 * property string           $requestText Request text
20
 *
21
 * @package Buddy
22
 */
23
class BuddyModel extends \Entity\KeyedModel{
24
25
  /**
26
   * Name of table for this entity
27
   *
28
   * @var string $tableName
29
   */
30
  protected $tableName = 'buddy';
31
  /**
32
   * Name of key field field in this table
33
   *
34
   * @var string $idFieldName
35
   */
36
  protected $idFieldName = 'BUDDY_ID';
37
  protected $exceptionClass = 'BuddyException';
38
  protected $entityContainerClass = 'Buddy\BuddyContainer';
39
40
  protected $properties = array(
41
    'dbId'           => array(
42
      P_DB_FIELD => 'BUDDY_ID',
43
    ),
44
    'playerSenderId' => array(
45
      P_DB_FIELD => 'BUDDY_SENDER_ID',
46
    ),
47
    'playerOwnerId'  => array(
48
      P_DB_FIELD => 'BUDDY_OWNER_ID',
49
    ),
50
    'buddyStatusId'  => array(
51
      P_DB_FIELD => 'BUDDY_STATUS',
52
    ),
53
    'requestText'    => array(
54
      P_DB_FIELD => 'BUDDY_REQUEST',
55
    ),
56
  );
57
58
  public function __construct(\Common\GlobalContainer $gc) {
59
    parent::__construct($gc);
60
    $this->extendProperties(
61
        array(
62
//      'dbId'           => array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
63
//        P_DB_FIELD => 'BUDDY_ID',
64
//      ),
65
      'playerSenderId' => array(
66
        P_DB_FIELD => 'BUDDY_SENDER_ID',
67
      ),
68
      'playerOwnerId'  => array(
69
        P_DB_FIELD => 'BUDDY_OWNER_ID',
70
      ),
71
      'buddyStatusId'  => array(
72
        P_DB_FIELD => 'BUDDY_STATUS',
73
      ),
74
      'requestText'    => array(
75
        P_DB_FIELD => 'BUDDY_REQUEST',
76
      ),
77
    )
78
79
    );
80
  }
81
82
  /**
83
   * @param BuddyContainer $cBuddy
84
   *
85
   * @return int
86
   */
87
  public function db_buddy_update_status($cBuddy) {
88
    return $this->rowOperator->doUpdateRowSetAffected(
89
      TABLE_BUDDY,
90
      array(
91
        'BUDDY_STATUS' => $cBuddy->buddyStatusId,
92
      ),
93
      array(
94
        'BUDDY_ID' => $cBuddy->dbId,
95
      )
96
    );
97
  }
98
99
  /**
100
   * @param $playerIdUnsafe
101
   * @param $newFriendIdUnsafe
102
   *
103
   * @throws BuddyException
104
   */
105
  public function db_buddy_check_relation($playerIdUnsafe, $newFriendIdUnsafe) {
106
    $playerIdSafe = idval($playerIdUnsafe);
107
    $newFriendIdSafe = idval($newFriendIdUnsafe);
108
109
    $result = $this->rowOperator->doSelectFetchValue(
110
      "SELECT `BUDDY_ID` 
111
      FROM `{{buddy}}` 
112
      WHERE
113
        (`BUDDY_SENDER_ID` = {$playerIdSafe} AND `BUDDY_OWNER_ID` = {$newFriendIdSafe})
114
        OR
115
        (`BUDDY_SENDER_ID` = {$newFriendIdSafe} AND `BUDDY_OWNER_ID` = {$playerIdSafe})
116
      LIMIT 1 
117
      FOR UPDATE;"
118
    );
119
120
    if (!empty($result)) {
121
      throw new BuddyException('buddy_err_adding_exists', ERR_WARNING);
122
    }
123
  }
124
125
  /**
126
   * @param mixed $playerId
127
   *
128
   * @return DbEmptyIterator|DbMysqliResultIterator
129
   */
130
  public function db_buddy_list_by_user($playerId) {
131
    return ($user_id = idval($playerId)) ? $this->rowOperator->doSelectIterator(
132
      "SELECT
133
      b.*,
134
      IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID) AS BUDDY_USER_ID,
135
      u.username AS BUDDY_USER_NAME,
136
      p.name AS BUDDY_PLANET_NAME,
137
      p.galaxy AS BUDDY_PLANET_GALAXY,
138
      p.system AS BUDDY_PLANET_SYSTEM,
139
      p.planet AS BUDDY_PLANET_PLANET,
140
      a.id AS BUDDY_ALLY_ID,
141
      a.ally_name AS BUDDY_ALLY_NAME,
142
      u.onlinetime
143
    FROM {{buddy}} AS b
144
      LEFT JOIN {{users}} AS u ON u.id = IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID)
145
      LEFT JOIN {{planets}} AS p ON p.id_owner = u.id AND p.id = id_planet
146
      LEFT JOIN {{alliance}} AS a ON a.id = u.ally_id
147
    WHERE (`BUDDY_OWNER_ID` = {$user_id}) OR `BUDDY_SENDER_ID` = {$user_id}
148
    ORDER BY BUDDY_STATUS, BUDDY_ID"
149
    ) : new DbEmptyIterator();
150
  }
151
152
  /**
153
   * @param BuddyContainer $cBuddy
154
   * @param BuddyParams    $params
155
   *
156
   * @throws BuddyException
157
   */
158
  public function accept($cBuddy, $params) {
159
    if ($params->mode != 'accept') {
160
      return;
161
    }
162
163
    if ($cBuddy->playerSenderId == $params->playerId) {
164
      throw new BuddyException('buddy_err_accept_own', ERR_ERROR);
165
    }
166
167
    if ($cBuddy->playerOwnerId != $params->playerId) {
168
      throw new BuddyException('buddy_err_accept_alien', ERR_ERROR);
169
    }
170
171
    if ($cBuddy->buddyStatusId == BUDDY_REQUEST_ACTIVE) {
172
      throw new BuddyException('buddy_err_accept_already', ERR_WARNING);
173
    }
174
175
    if ($cBuddy->buddyStatusId == BUDDY_REQUEST_DENIED) {
176
      throw new BuddyException('buddy_err_accept_denied', ERR_ERROR);
177
    }
178
179
    if ($cBuddy->buddyStatusId != BUDDY_REQUEST_WAITING) {
180
      throw new BuddyException('buddy_err_unknown_status', ERR_ERROR);
181
    }
182
183
    $cBuddy->buddyStatusId = BUDDY_REQUEST_ACTIVE;
184
    $result = $this->db_buddy_update_status($cBuddy);
185
    if ($result) {
186
      DBStaticMessages::msgSendFromPlayerBuddy($params, 'buddy_msg_accept_title', 'buddy_msg_accept_text');
187
      throw new BuddyException('buddy_err_accept_none', ERR_NONE);
188
    } else {
189
      throw new BuddyException('buddy_err_accept_internal', ERR_ERROR);
190
    }
191
  }
192
193
  /**
194
   * Declining buddy request
195
   *
196
   * If it is own request - it will be deleted
197
   *
198
   * @param BuddyContainer $cBuddy
199
   * @param BuddyParams    $params
200
   *
201
   * @throws BuddyException
202
   */
203
  public function deleteRequest($cBuddy, $params) {
204
    if ($params->mode != 'delete') {
205
      return;
206
    }
207
208
    $playerId = $params->playerId;
209
210
    if ($cBuddy->playerSenderId != $params->playerId && $cBuddy->playerOwnerId != $params->playerId) {
211
      throw new BuddyException('buddy_err_delete_alien', ERR_ERROR);
212
    }
213
214
    if ($cBuddy->buddyStatusId == BUDDY_REQUEST_ACTIVE) {
215
      // Existing friendship
216
      $params->newFriendIdSafe = $cBuddy->playerSenderId == $playerId ? $cBuddy->playerOwnerId : $cBuddy->playerSenderId;
217
      DBStaticMessages::msgSendFromPlayerBuddy($params, 'buddy_msg_unfriend_title', 'buddy_msg_unfriend_text');
218
219
      $this->rowOperator->deleteById($this, $cBuddy->dbId);
220
      throw new BuddyException('buddy_err_unfriend_none', ERR_NONE);
221
    } elseif ($cBuddy->playerSenderId == $playerId) {
222
      // Player's outcoming request - either denied or waiting
223
      $this->rowOperator->deleteById($this, $cBuddy->dbId);
224
      throw new BuddyException('buddy_err_delete_own', ERR_NONE);
225
    } elseif ($cBuddy->buddyStatusId == BUDDY_REQUEST_WAITING) {
226
      // Deny incoming request
227
      DBStaticMessages::msgSendFromPlayerBuddy($params, 'buddy_msg_deny_title', 'buddy_msg_deny_text');
228
229
      $cBuddy->buddyStatusId = BUDDY_REQUEST_DENIED;
230
      $this->db_buddy_update_status($cBuddy);
231
      throw new BuddyException('buddy_err_deny_none', ERR_NONE);
232
    }
233
  }
234
235
  /**
236
   * @param int    $newFriendIdSafe
237
   * @param string $newFriendNameUnsafe
238
   *
239
   * @return array|bool|false|\mysqli_result|null
240
   */
241
  protected function getNewFriend($newFriendIdSafe, $newFriendNameUnsafe) {
242
    $new_friend_row = array();
243
    if ($newFriendIdSafe) {
244
      $new_friend_row = DBStaticUser::db_user_by_id($newFriendIdSafe, true, '`id`, `username`');
245
    } elseif ($newFriendNameUnsafe) {
246
      $new_friend_row = DBStaticUser::db_user_by_username($newFriendNameUnsafe, true, '`id`, `username`');
247
    }
248
    if (empty($new_friend_row['id'])) {
249
      throw new BuddyException('buddy_err_unknown_player', ERR_ERROR);
250
    }
251
252
    return $new_friend_row;
253
  }
254
255
  /**
256
   * @param BuddyParams $params
257
   *
258
   * @throws BuddyException
259
   */
260
  public function beFriend($params) {
261
    if (empty($params->newFriendIdSafe) && empty($params->new_friend_name_unsafe)) {
262
      return;
263
    }
264
265
    $new_friend_row = $this->getNewFriend($params->newFriendIdSafe, $params->new_friend_name_unsafe);
266
    if ($new_friend_row['id'] == $params->playerId) {
267
      throw new BuddyException('buddy_err_adding_self', ERR_ERROR);
268
    }
269
270
    $this->db_buddy_check_relation($params->playerId, $new_friend_row['id']);
271
    $params->newFriendIdSafe = $new_friend_row['id'];
272
    DBStaticMessages::msgSendFromPlayerBuddy($params, 'buddy_msg_adding_title', 'buddy_msg_adding_text');
273
274
    $cBuddy = $this->buildContainer();
275
    $cBuddy->playerSenderId = $params->playerId;
276
    $cBuddy->playerOwnerId = $new_friend_row['id'];
277
    $cBuddy->buddyStatusId = BUDDY_REQUEST_WAITING;
278
    $cBuddy->requestText = $params->request_text_unsafe;
279
280
    $this->exportRowNoId($cBuddy);
281
    $cBuddy->dbId = $this->rowOperator->insert($this, $cBuddy->row);
282
    throw new BuddyException('buddy_err_adding_none', ERR_NONE);
283
  }
284
285
  /**
286
   * @param BuddyParams $params
287
   *
288
   * @throws BuddyException
289
   */
290
  public function route($params) {
291
    // Trying to load buddy record with supplied dbId
292
    if ($params->buddy_id) {
293
      $cBuddy = $this->loadById($params->buddy_id);
294
      if (!$cBuddy) {
295
        throw new BuddyException('buddy_err_not_exist', ERR_ERROR);
296
      }
297
298
      // Trying to accept buddy request
299
      $this->accept($cBuddy, $params);
300
      // Trying to decline buddy request. If it's own request - it will be deleted
301
      $this->deleteRequest($cBuddy, $params);
302
    } else {
303
      $this->beFriend($params);
304
    }
305
  }
306
307
}
308