Passed
Push — trunk ( 780f52...305828 )
by SuperNova.WS
04:36
created

Alliance::getMemberList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 9.4285
ccs 0
cts 5
cp 0
crap 6
1
<?php
2
/**
3
 * Created by Gorlum 28.11.2017 6:01
4
 */
5
6
namespace Alliance;
7
8
use Common\GlobalContainer;
9
use \Exception;
10
use \HelperString;
11
12
/**
13
 * Class Alliance
14
 *
15
 * Implements Alliance entity
16
 *
17
 * @package Alliance
18
 */
19
class Alliance extends RecordAlliance {
20
  const OWNER_INDEX = -1;
21
  const DEFAULT_INDEX = 0;
22
  const RIGHTS_ALL = [
23
    0 => 'name',
24
    1 => 'mail',
25
    2 => 'online',
26
    3 => 'invite',
27
    4 => 'kick',
28
    5 => 'admin',
29
    6 => 'forum',
30
    7 => 'diplomacy'
31
  ];
32
  const RIGHT_WEIGHTS = [
33
    'mail'      => 3,
34
    'online'    => 4,
35
    'invite'    => 1,
36
    'kick'      => 10,
37
    'admin'     => 99,
38
    'forum'     => 0,
39
    'diplomacy' => 5,
40
  ];
41
42
43
  /**
44
   * @var AllianceTitleList $titles
45
   */
46
  protected $titles;
47
  /**
48
   * @var AllianceMemberList $members
49
   */
50
  protected $members;
51
52
  /**
53
   * Alliance constructor.
54
   *
55
   * @param GlobalContainer|null $services
56
   */
57
  public function __construct(GlobalContainer $services = null) {
58
    parent::__construct($services);
59
  }
60
61
  /**
62
   * @param array $properties
63
   */
64
  protected function fromProperties(array $properties) {
65
    parent::fromProperties($properties);
66
67
    $this->titles = new AllianceTitleList($this);
68
  }
69
70
  /**
71
   * @return AllianceMemberList
72
   */
73
  public function getMemberList() {
74
    if (!isset($this->members)) {
75
      $this->members = new AllianceMemberList(static::$db, $this);
76
    }
77
78
    return $this->members;
79
  }
80
81
  /**
82
   * List of titles
83
   *
84
   * @return AllianceTitleList
85
   */
86
  public function getTitleList() {
87
    return $this->titles;
88
  }
89
90
  /**
91
   * Pass alliance to a member
92
   *
93
   * @param AllianceMember $newOwnerMember
94
   *
95
   * @return bool
96
   * @throws Exception
97
   */
98
  public function pass(AllianceMember $newOwnerMember) {
99
    try {
100
      sn_db_transaction_start();
101
102
      if ($newOwnerMember->isOwner()) {
103
        throw new Exception('{ Указанный пользователь уже является владельцем указанного Альянса }', ERR_NOTICE);
104
      }
105
106
      if (!empty($oldOwnerMember = $this->members->getOwner())) {
107
        if (!$oldOwnerMember->changeTitle($this->titles->getTitle(static::DEFAULT_INDEX))) {
108
          throw new Exception('{ Ошибка изменения ранга у старого владельца }', ERR_ERROR);
109
        }
110
      }
111
112
      if (!$newOwnerMember->changeTitle($this->titles->getTitle(static::OWNER_INDEX))) {
113
        throw new Exception('{ Ошибка изменения ранга у нового владельца }', ERR_ERROR);
114
      }
115
116
      $this->ownerId = $newOwnerMember->getPlayerId();
117
      if (!$this->update()) {
118
        throw new Exception('{ Ошибка изменения владельца Альянса }', ERR_ERROR);
119
      }
120
121
      sn_db_transaction_commit();
122
    } catch (Exception $e) {
123
      sn_db_transaction_rollback();
124
125
      throw $e;
126
    }
127
128
    return true;
129
  }
130
131
  /**
132
   * @return array
133
   */
134
  public function asPtl() {
135
    $ownerName = $this->getMemberList()->getOwner() instanceof AllianceMember ? $this->getMemberList()->getOwner()->getMemberName() : '';
136
137
    return
138
      $this->ptlArray()
139
      + [
140
        '.'                => [
141
          'title' => $this->titles->asPtl()
142
        ],
143
        'OWNER_NAME_SAFE'  => HelperString::htmlSafe($ownerName),
144
        'CREATED_TEXT'     => date(FMT_DATE_TIME_SQL, $this->createdUnixTime),
145
        'STAT_POINTS_TEXT' => HelperString::numberFloorAndFormat($this->statPoints),
146
      ];
147
  }
148
149
}
150