Completed
Push — work-fleets ( d9c01a...3f92e6 )
by SuperNova.WS
07:11
created

BuddyModel   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 261
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 25
Bugs 1 Features 0
Metric Value
c 25
b 1
f 0
dl 0
loc 261
rs 9.6
ccs 0
cts 144
cp 0
wmc 32
lcom 1
cbo 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A db_buddy_update_status() 0 11 1
A db_buddy_check_relation() 0 19 2
A db_buddy_list_by_user() 0 21 2
C accept() 0 34 8
C deleteRequest() 0 31 8
A getNewFriend() 0 13 4
B beFriend() 0 24 4
A route() 0 16 3
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
  private $newProperties = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $newProperties is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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
  /**
59
   * @param BuddyContainer $cBuddy
60
   *
61
   * @return int
62
   */
63
  public function db_buddy_update_status($cBuddy) {
64
    return $this->rowOperator->doUpdateRowSetAffected(
65
      TABLE_BUDDY,
66
      array(
67
        'BUDDY_STATUS' => $cBuddy->buddyStatusId,
68
      ),
69
      array(
70
        'BUDDY_ID' => $cBuddy->dbId,
71
      )
72
    );
73
  }
74
75
  /**
76
   * @param $playerIdUnsafe
77
   * @param $newFriendIdUnsafe
78
   *
79
   * @throws BuddyException
80
   */
81
  public function db_buddy_check_relation($playerIdUnsafe, $newFriendIdUnsafe) {
82
    $playerIdSafe = idval($playerIdUnsafe);
83
    $newFriendIdSafe = idval($newFriendIdUnsafe);
84
85
    $result = $this->rowOperator->doSelectFetchValue(
86
      "SELECT `BUDDY_ID` 
87
      FROM `{{buddy}}` 
88
      WHERE
89
        (`BUDDY_SENDER_ID` = {$playerIdSafe} AND `BUDDY_OWNER_ID` = {$newFriendIdSafe})
90
        OR
91
        (`BUDDY_SENDER_ID` = {$newFriendIdSafe} AND `BUDDY_OWNER_ID` = {$playerIdSafe})
92
      LIMIT 1 
93
      FOR UPDATE;"
94
    );
95
96
    if (!empty($result)) {
97
      throw new BuddyException('buddy_err_adding_exists', ERR_WARNING);
98
    }
99
  }
100
101
  /**
102
   * @param mixed $playerId
103
   *
104
   * @return DbEmptyIterator|DbMysqliResultIterator
105
   */
106
  public function db_buddy_list_by_user($playerId) {
107
    return ($user_id = idval($playerId)) ? $this->rowOperator->doSelectIterator(
108
      "SELECT
109
      b.*,
110
      IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID) AS BUDDY_USER_ID,
111
      u.username AS BUDDY_USER_NAME,
112
      p.name AS BUDDY_PLANET_NAME,
113
      p.galaxy AS BUDDY_PLANET_GALAXY,
114
      p.system AS BUDDY_PLANET_SYSTEM,
115
      p.planet AS BUDDY_PLANET_PLANET,
116
      a.id AS BUDDY_ALLY_ID,
117
      a.ally_name AS BUDDY_ALLY_NAME,
118
      u.onlinetime
119
    FROM {{buddy}} AS b
120
      LEFT JOIN {{users}} AS u ON u.id = IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID)
121
      LEFT JOIN {{planets}} AS p ON p.id_owner = u.id AND p.id = id_planet
122
      LEFT JOIN {{alliance}} AS a ON a.id = u.ally_id
123
    WHERE (`BUDDY_OWNER_ID` = {$user_id}) OR `BUDDY_SENDER_ID` = {$user_id}
124
    ORDER BY BUDDY_STATUS, BUDDY_ID"
125
    ) : new DbEmptyIterator();
126
  }
127
128
  /**
129
   * @param BuddyContainer $cBuddy
130
   * @param BuddyParams    $params
131
   *
132
   * @throws BuddyException
133
   */
134
  public function accept($cBuddy, $params) {
135
    if ($params->mode != 'accept') {
136
      return;
137
    }
138
139
    if ($cBuddy->playerSenderId == $params->playerId) {
140
      throw new BuddyException('buddy_err_accept_own', ERR_ERROR);
141
    }
142
143
    if ($cBuddy->playerOwnerId != $params->playerId) {
144
      throw new BuddyException('buddy_err_accept_alien', ERR_ERROR);
145
    }
146
147
    if ($cBuddy->buddyStatusId == BUDDY_REQUEST_ACTIVE) {
148
      throw new BuddyException('buddy_err_accept_already', ERR_WARNING);
149
    }
150
151
    if ($cBuddy->buddyStatusId == BUDDY_REQUEST_DENIED) {
152
      throw new BuddyException('buddy_err_accept_denied', ERR_ERROR);
153
    }
154
155
    if ($cBuddy->buddyStatusId != BUDDY_REQUEST_WAITING) {
156
      throw new BuddyException('buddy_err_unknown_status', ERR_ERROR);
157
    }
158
159
    $cBuddy->buddyStatusId = BUDDY_REQUEST_ACTIVE;
160
    $result = $this->db_buddy_update_status($cBuddy);
161
    if ($result) {
162
      DBStaticMessages::msgSendFromPlayerBuddy($params, 'buddy_msg_accept_title', 'buddy_msg_accept_text');
163
      throw new BuddyException('buddy_err_accept_none', ERR_NONE);
164
    } else {
165
      throw new BuddyException('buddy_err_accept_internal', ERR_ERROR);
166
    }
167
  }
168
169
  /**
170
   * Declining buddy request
171
   *
172
   * If it is own request - it will be deleted
173
   *
174
   * @param BuddyContainer $cBuddy
175
   * @param BuddyParams    $params
176
   *
177
   * @throws BuddyException
178
   */
179
  public function deleteRequest($cBuddy, $params) {
180
    if ($params->mode != 'delete') {
181
      return;
182
    }
183
184
    $playerId = $params->playerId;
185
186
    if ($cBuddy->playerSenderId != $params->playerId && $cBuddy->playerOwnerId != $params->playerId) {
187
      throw new BuddyException('buddy_err_delete_alien', ERR_ERROR);
188
    }
189
190
    if ($cBuddy->buddyStatusId == BUDDY_REQUEST_ACTIVE) {
191
      // Existing friendship
192
      $params->newFriendIdSafe = $cBuddy->playerSenderId == $playerId ? $cBuddy->playerOwnerId : $cBuddy->playerSenderId;
193
      DBStaticMessages::msgSendFromPlayerBuddy($params, 'buddy_msg_unfriend_title', 'buddy_msg_unfriend_text');
194
195
      $this->rowOperator->deleteById($this, $cBuddy->dbId);
196
      throw new BuddyException('buddy_err_unfriend_none', ERR_NONE);
197
    } elseif ($cBuddy->playerSenderId == $playerId) {
198
      // Player's outcoming request - either denied or waiting
199
      $this->rowOperator->deleteById($this, $cBuddy->dbId);
200
      throw new BuddyException('buddy_err_delete_own', ERR_NONE);
201
    } elseif ($cBuddy->buddyStatusId == BUDDY_REQUEST_WAITING) {
202
      // Deny incoming request
203
      DBStaticMessages::msgSendFromPlayerBuddy($params, 'buddy_msg_deny_title', 'buddy_msg_deny_text');
204
205
      $cBuddy->buddyStatusId = BUDDY_REQUEST_DENIED;
206
      $this->db_buddy_update_status($cBuddy);
207
      throw new BuddyException('buddy_err_deny_none', ERR_NONE);
208
    }
209
  }
210
211
  /**
212
   * @param int    $newFriendIdSafe
213
   * @param string $newFriendNameUnsafe
214
   *
215
   * @return array|bool|false|\mysqli_result|null
216
   */
217
  protected function getNewFriend($newFriendIdSafe, $newFriendNameUnsafe) {
218
    $new_friend_row = array();
219
    if ($newFriendIdSafe) {
220
      $new_friend_row = DBStaticUser::db_user_by_id($newFriendIdSafe, true, '`id`, `username`');
221
    } elseif ($newFriendNameUnsafe) {
222
      $new_friend_row = DBStaticUser::db_user_by_username($newFriendNameUnsafe, true, '`id`, `username`');
223
    }
224
    if (empty($new_friend_row['id'])) {
225
      throw new BuddyException('buddy_err_unknown_player', ERR_ERROR);
226
    }
227
228
    return $new_friend_row;
229
  }
230
231
  /**
232
   * @param BuddyParams $params
233
   *
234
   * @throws BuddyException
235
   */
236
  public function beFriend($params) {
237
    if (empty($params->newFriendIdSafe) && empty($params->new_friend_name_unsafe)) {
238
      return;
239
    }
240
241
    $new_friend_row = $this->getNewFriend($params->newFriendIdSafe, $params->new_friend_name_unsafe);
242
    if ($new_friend_row['id'] == $params->playerId) {
243
      throw new BuddyException('buddy_err_adding_self', ERR_ERROR);
244
    }
245
246
    $this->db_buddy_check_relation($params->playerId, $new_friend_row['id']);
247
    $params->newFriendIdSafe = $new_friend_row['id'];
248
    DBStaticMessages::msgSendFromPlayerBuddy($params, 'buddy_msg_adding_title', 'buddy_msg_adding_text');
249
250
    $cBuddy = $this->buildContainer();
251
    $cBuddy->playerSenderId = $params->playerId;
252
    $cBuddy->playerOwnerId = $new_friend_row['id'];
253
    $cBuddy->buddyStatusId = BUDDY_REQUEST_WAITING;
254
    $cBuddy->requestText = $params->request_text_unsafe;
255
256
    $this->exportRowNoId($cBuddy);
257
    $cBuddy->dbId = $this->rowOperator->insert($this, $cBuddy->row);
258
    throw new BuddyException('buddy_err_adding_none', ERR_NONE);
259
  }
260
261
  /**
262
   * @param BuddyParams $params
263
   *
264
   * @throws BuddyException
265
   */
266
  public function route($params) {
267
    // Trying to load buddy record with supplied dbId
268
    if ($params->buddy_id) {
269
      $cBuddy = $this->loadById($params->buddy_id);
270
      if (!$cBuddy) {
271
        throw new BuddyException('buddy_err_not_exist', ERR_ERROR);
272
      }
273
274
      // Trying to accept buddy request
275
      $this->accept($cBuddy, $params);
276
      // Trying to decline buddy request. If it's own request - it will be deleted
277
      $this->deleteRequest($cBuddy, $params);
278
    } else {
279
      $this->beFriend($params);
280
    }
281
  }
282
283
}
284