Completed
Push — work-fleets ( bd15ac...5c3a01 )
by SuperNova.WS
06:16
created

BuddyModel::deleteRequest()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

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

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
258
      // Trying to decline buddy request. If it's own request - it will be deleted
259
      $this->deleteRequest($cBuddy, $params);
0 ignored issues
show
Compatibility introduced by
$cBuddy of type object<IEntityContainer> is not a sub-type of object<Buddy\BuddyContainer>. It seems like you assume a concrete implementation of the interface IEntityContainer to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
260
    } else {
261
      // New request?
262
      // TODO !!!!!!!!!!!!!!!!!!
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% 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...
263
      $this->beFriend($params);
264
    }
265
  }
266
267
}
268