Completed
Push — work-fleets ( 0f036f...867546 )
by SuperNova.WS
06:49
created

BuddyModel::isContainerEmpty()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 10
ccs 0
cts 10
cp 0
crap 20
rs 9.2
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
  protected static $tableName = 'buddy';
23
  protected static $idField = 'BUDDY_ID';
24
  protected static $exceptionClass = 'BuddyException';
25
26
27
  /**
28
   * @param BuddyContainer $cBuddy
29
   *
30
   * @return int
31
   */
32
  public function db_buddy_update_status($cBuddy) {
33
    $db = $cBuddy->getDbStatic();
34
    $db->doUpdateRowSet(
35
      TABLE_BUDDY,
36
      array(
37
        'BUDDY_STATUS' => $cBuddy->buddyStatusId,
38
      ),
39
      array(
40
        'BUDDY_ID' => $cBuddy->dbId,
0 ignored issues
show
Documentation introduced by
The property dbId does not exist on object<Buddy\BuddyContainer>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
41
      )
42
    );
43
44
    return $db->db_affected_rows();
45
  }
46
47
  /**
48
   * @param BuddyContainer $cBuddy
49
   *
50
   * @throws BuddyException
51
   */
52
  public function db_buddy_check_relation($cBuddy) {
53
    $playerIdSafe = idval($cBuddy->playerId);
54
    $newFriendIdSafe = idval($cBuddy->newFriendIdSafe);
55
56
    $result = $cBuddy->getDbStatic()->doSelectFetchValue(
57
      "SELECT `BUDDY_ID` 
58
      FROM `{{buddy}}` 
59
      WHERE
60
        (`BUDDY_SENDER_ID` = {$playerIdSafe} AND `BUDDY_OWNER_ID` = {$newFriendIdSafe})
61
        OR
62
        (`BUDDY_SENDER_ID` = {$newFriendIdSafe} AND `BUDDY_OWNER_ID` = {$playerIdSafe})
63
      LIMIT 1 
64
      FOR UPDATE;"
65
    );
66
67
    if (!empty($result)) {
68
      throw new BuddyException('buddy_err_adding_exists', ERR_WARNING);
69
    }
70
  }
71
72
  /**
73
   * @param BuddyContainer $cBuddy
74
   *
75
   * @return DbEmptyIterator|DbMysqliResultIterator
76
   */
77
  public function db_buddy_list_by_user($cBuddy) {
78
    return ($user_id = idval($cBuddy->playerId)) ? $cBuddy->getDbStatic()->doSelectIterator(
79
      "SELECT
80
      b.*,
81
      IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID) AS BUDDY_USER_ID,
82
      u.username AS BUDDY_USER_NAME,
83
      p.name AS BUDDY_PLANET_NAME,
84
      p.galaxy AS BUDDY_PLANET_GALAXY,
85
      p.system AS BUDDY_PLANET_SYSTEM,
86
      p.planet AS BUDDY_PLANET_PLANET,
87
      a.id AS BUDDY_ALLY_ID,
88
      a.ally_name AS BUDDY_ALLY_NAME,
89
      u.onlinetime
90
    FROM {{buddy}} AS b
91
      LEFT JOIN {{users}} AS u ON u.id = IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID)
92
      LEFT JOIN {{planets}} AS p ON p.id_owner = u.id AND p.id = id_planet
93
      LEFT JOIN {{alliance}} AS a ON a.id = u.ally_id
94
    WHERE (`BUDDY_OWNER_ID` = {$user_id}) OR `BUDDY_SENDER_ID` = {$user_id}
95
    ORDER BY BUDDY_STATUS, BUDDY_ID"
96
    ) : new DbEmptyIterator();
97
  }
98
99
  /**
100
   * @param BuddyContainer $cBuddy
101
   *
102
   * @throws BuddyException
103
   */
104
  public function accept($cBuddy) {
105
    if ($cBuddy->mode != 'accept') {
106
      return;
107
    }
108
109
    if ($cBuddy->playerSenderId == $cBuddy->playerId) {
110
      throw new BuddyException('buddy_err_accept_own', ERR_ERROR);
111
    }
112
113
    if ($cBuddy->playerOwnerId != $cBuddy->playerId) {
114
      throw new BuddyException('buddy_err_accept_alien', ERR_ERROR);
115
    }
116
117
    if ($cBuddy->buddyStatusId == BUDDY_REQUEST_ACTIVE) {
118
      throw new BuddyException('buddy_err_accept_already', ERR_WARNING);
119
    }
120
121
    if ($cBuddy->buddyStatusId == BUDDY_REQUEST_DENIED) {
122
      throw new BuddyException('buddy_err_accept_denied', ERR_ERROR);
123
    }
124
125
    if ($cBuddy->buddyStatusId != BUDDY_REQUEST_WAITING) {
126
      throw new BuddyException('buddy_err_unknown_status', ERR_ERROR);
127
    }
128
129
    $cBuddy->buddyStatusId = BUDDY_REQUEST_ACTIVE;
130
    $result = $this->db_buddy_update_status($cBuddy);
131
    if ($result) {
132
      DBStaticMessages::msgSendFromPlayerBuddy($cBuddy, 'buddy_msg_accept_title', 'buddy_msg_accept_text');
133
      throw new BuddyException('buddy_err_accept_none', ERR_NONE);
134
    } else {
135
      throw new BuddyException('buddy_err_accept_internal', ERR_ERROR);
136
    }
137
  }
138
139
  /**
140
   * Declining buddy request
141
   *
142
   * If it is own request - it will be deleted
143
   *
144
   * @param BuddyContainer $cBuddy
145
   *
146
   * @throws BuddyException
147
   */
148
  public function decline($cBuddy) {
149
    if ($cBuddy->mode != 'delete') {
150
      return;
151
    }
152
153
    $playerId = $cBuddy->playerId;
154
155
    if ($cBuddy->playerSenderId != $cBuddy->playerId && $cBuddy->playerOwnerId != $cBuddy->playerId) {
156
      throw new BuddyException('buddy_err_delete_alien', ERR_ERROR);
157
    }
158
159
    if ($cBuddy->buddyStatusId == BUDDY_REQUEST_ACTIVE) {
160
      // Existing friendship
161
      $cBuddy->newFriendIdSafe = $cBuddy->playerSenderId == $playerId ? $cBuddy->playerOwnerId : $cBuddy->playerSenderId;
162
      DBStaticMessages::msgSendFromPlayerBuddy($cBuddy, 'buddy_msg_unfriend_title', 'buddy_msg_unfriend_text');
163
164
      $cBuddy->delete();
165
      throw new BuddyException('buddy_err_unfriend_none', ERR_NONE);
166
    } elseif ($cBuddy->playerSenderId == $playerId) {
167
      // Player's outcoming request - either denied or waiting
168
      $cBuddy->delete();
169
      throw new BuddyException('buddy_err_delete_own', ERR_NONE);
170
    } elseif ($cBuddy->buddyStatusId == BUDDY_REQUEST_WAITING) {
171
      // Deny incoming request
172
      DBStaticMessages::msgSendFromPlayerBuddy($cBuddy, 'buddy_msg_deny_title', 'buddy_msg_deny_text');
173
174
      $cBuddy->buddyStatusId = BUDDY_REQUEST_DENIED;
175
      $this->db_buddy_update_status($cBuddy);
176
      throw new BuddyException('buddy_err_deny_none', ERR_NONE);
177
    }
178
  }
179
180
  /**
181
   * @param int    $newFriendIdSafe
182
   * @param string $newFriendNameUnsafe
183
   *
184
   * @return array|bool|false|\mysqli_result|null
185
   */
186
  protected function getNewFriend($newFriendIdSafe, $newFriendNameUnsafe) {
187
    $new_friend_row = array();
188
    if ($newFriendIdSafe) {
189
      $new_friend_row = DBStaticUser::db_user_by_id($newFriendIdSafe, true, '`id`, `username`');
190
    } elseif ($newFriendNameUnsafe) {
191
      $new_friend_row = DBStaticUser::db_user_by_username($newFriendNameUnsafe, true, '`id`, `username`');
192
    }
193
194
    return $new_friend_row;
195
  }
196
197
  /**
198
   * @param BuddyContainer $cBuddy
199
   *
200
   * @throws BuddyException
201
   */
202
  public function beFriend($cBuddy) {
203
    if (empty($cBuddy->newFriendIdSafe) && empty($cBuddy->new_friend_name_unsafe)) {
204
      return;
205
    }
206
207
    $new_friend_row = $this->getNewFriend($cBuddy->newFriendIdSafe, $cBuddy->new_friend_name_unsafe);
208
209
    if (empty($new_friend_row) || empty($new_friend_row['id'])) {
210
      throw new BuddyException('buddy_err_unknown_player', ERR_ERROR);
211
    }
212
213
    if ($new_friend_row['id'] == $cBuddy->playerId) {
214
      throw new BuddyException('buddy_err_adding_self', ERR_ERROR);
215
    }
216
217
    $cBuddy->newFriendIdSafe = $new_friend_row['id'];
218
    $this->db_buddy_check_relation($cBuddy);
219
    DBStaticMessages::msgSendFromPlayerBuddy($cBuddy, 'buddy_msg_adding_title', 'buddy_msg_adding_text');
220
221
    $cBuddy->playerSenderId = $cBuddy->playerId;
222
    $cBuddy->playerOwnerId = $new_friend_row['id'];
223
    $cBuddy->buddyStatusId = BUDDY_REQUEST_WAITING;
224
225
    $cBuddy->insert();
226
    throw new BuddyException('buddy_err_adding_none', ERR_NONE);
227
  }
228
229
  /**
230
   * @param BuddyContainer $cBuddy
231
   *
232
   * @throws BuddyException
233
   */
234
  public function route($cBuddy) {
235
    // Trying to load buddy record with supplied dbId
236
    if ($cBuddy->buddy_id) {
237
      $cBuddy->dbId = $cBuddy->buddy_id;
0 ignored issues
show
Documentation introduced by
The property dbId does not exist on object<Buddy\BuddyContainer>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
238
      if (!$cBuddy->loadTry()) {
239
        throw new BuddyException('buddy_err_not_exist', ERR_ERROR);
240
      }
241
    }
242
243
    // Trying to accept buddy request
244
    $this->accept($cBuddy);
245
    // Trying to decline buddy request. If it's own request - it will be deleted
246
    $this->decline($cBuddy);
247
    // New request?
248
    $this->beFriend($cBuddy);
249
  }
250
251
}
252