Test Failed
Push — trunk ( db071f...8d464a )
by SuperNova.WS
06:33
created

AllianceStatic::passAlliance()   D

Complexity

Conditions 9
Paths 23

Size

Total Lines 41
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 24
nc 23
nop 2
dl 0
loc 41
rs 4.909
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by Gorlum 15.06.2017 11:56
4
 */
5
6
namespace Alliance;
7
8
9
use Player\TablePlayer;
10
11
class AllianceStatic {
12
13
  public static function parseTitleList($string) {
14
    global $ally_rights;
15
16
    $result = [];
17
18
    $titleList = explode(';', $string);
19
    foreach ($titleList as $titleIndex => $titleString) {
20
      if (empty($titleString)) {
21
        continue;
22
      }
23
      $accessList = explode(',', $titleString);
24
      $result[$titleIndex] = array_combine($ally_rights, $accessList);
25
    }
26
27
    return $result;
28
  }
29
30
  public static function getMemberList($allyId) {
31
    $allyId_safe = \classSupernova::$db->db_escape($allyId);
32
33
    return db_user_list("`ally_id`= {$allyId_safe} ORDER BY `ally_rank_id`", false,
0 ignored issues
show
Deprecated Code introduced by
The function db_user_list() has been deprecated.

This function has been deprecated.

Loading history...
34
      'id, username, galaxy, system, planet, onlinetime, ally_rank_id, ally_register_time, total_points');
35
  }
36
37
  public static function compileTitleRights(&$titleList) {
38
    foreach ($titleList as &$titleDesc) {
39
      $titleRights = [];
40
      foreach ($titleDesc as $rightId => $rightValue) {
41
        if ($rightValue == 1) {
42
          $titleRights[] = $rightId;
43
        }
44
      }
45
      $titleDesc['rights'] = implode(',', $titleRights);
46
    }
47
48
  }
49
50
  public static function titleMembers($memberList, TableAlliance $alliance) {
51
    global $ally_rights;
52
53
    $copy = $ally_rights;
54
    array_shift($copy);
55
56
    $result = [];
57
    if (empty($memberList)) {
58
      return $result;
59
    }
60
61
    $titleList = self::parseTitleList($alliance->titleList);
62
    self::compileTitleRights($titleList);
63
64
65
    foreach ($memberList as $playerRecord) {
66
      $temp = [
67
        'PLAYER_ID'    => $playerRecord['id'],
68
        'PLAYER_NAME'  => $playerRecord['username'],
69
        'ONLINE'       => $playerRecord['onlinetime'],
70
        'ONLINE_SQL'   => date(FMT_DATE_TIME_SQL, $playerRecord['onlinetime']),
71
        'VACATION'     => $playerRecord['vacation'],
72
        'VACATION_SQL' => date(FMT_DATE_TIME_SQL, $playerRecord['vacation']),
73
        'BAN'          => $playerRecord['banaday'],
74
        'BAN_SQL'      => date(FMT_DATE_TIME_SQL, $playerRecord['banaday']),
75
        'TITLE'        => $titleList[$playerRecord['ally_rank_id']]['name'],
76
        'TITLE_ID'     => $playerRecord['ally_rank_id'],
77
        'RIGHTS'       => $titleList[$playerRecord['ally_rank_id']]['rights'],
78
      ];
79
      if ($playerRecord['id'] == $alliance->ownerId) {
80
        $temp = array_merge($temp, [
81
          'TITLE'    => $alliance->ownerRankName,
82
          'TITLE_ID' => -1,
83
          'RIGHTS'   => implode(',', $copy),
84
          'OWNER'    => true,
85
        ]);
86
      }
87
      $result[] = $temp;
88
    }
89
90
    return $result;
91
  }
92
93
94
  public static function passAlliance($allyId, $newOwnerId) {
95
    try {
96
      sn_db_transaction_start();
97
      if (empty($alliance = \Alliance\TableAlliance::findOne($allyId))) {
98
        throw new \Exception('{ Альянс с указанным ID не найден }', ERR_ERROR);
99
      }
100
101
      if ($newOwnerId == $alliance->ownerId) {
102
        throw new \Exception('{ Указанный пользователь уже является владельцем указанного Альянса }', ERR_NOTICE);
103
      }
104
105
      if (!empty($oldOwner = TablePlayer::findOne($alliance->ownerId))) {
106
        $oldOwner->ally_rank_id = 0;
0 ignored issues
show
Documentation introduced by
The property ally_rank_id does not exist on object<Player\TablePlayer>. 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...
107
        if (!$oldOwner->update()) {
108
          throw new \Exception('{ Ошибка изменения ранга у старого владельца }', ERR_ERROR);
109
        }
110
      }
111
112
      if (empty($newOwner = TablePlayer::findOne($newOwnerId))) {
113
        throw new \Exception('{ Новый владелец Альянса не найден }', ERR_ERROR);
114
      }
115
116
      $newOwner->ally_rank_id = 0;
0 ignored issues
show
Documentation introduced by
The property ally_rank_id does not exist on object<Player\TablePlayer>. 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...
117
      if (!$newOwner->update()) {
118
        throw new \Exception('{ Ошибка изменения ранга у нового владельца }', ERR_ERROR);
119
      }
120
121
      $alliance->ownerId = $newOwnerId;
122
      if (!$alliance->update()) {
123
        throw new \Exception('{ Ошибка изменения владельца Альянса }', ERR_ERROR);
124
      }
125
126
      sn_db_transaction_commit();
127
    } catch (\Exception $e) {
128
      sn_db_transaction_rollback();
129
130
      throw $e;
131
    }
132
133
    return true;
134
  }
135
136
}
137