Completed
Push — trunk ( e8681e...314ee9 )
by SuperNova.WS
13:57
created

Governor::hire()   D

Complexity

Conditions 9
Paths 4

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 25
nc 4
nop 1
dl 0
loc 38
ccs 0
cts 32
cp 0
crap 90
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Created by Gorlum 08.01.2018 13:23
5
 */
6
7
namespace Unit;
8
9
use \classSupernova;
10
use Planet\Planet;
11
12
class Governor {
13
//  protected $type = UNIT_GOVERNOR_PRIMARY;
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% 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...
14
//  protected $typeIdField = 'PLANET_GOVERNOR_ID';
15
//  protected $typeLevelField = 'PLANET_GOVERNOR_LEVEL';
16
17
  protected $id = 0;
18
  protected $level = 0;
19
20
  /**
21
   * @var RecordUnit $unit
22
   */
23
  protected $unit;
24
25
  /**
26
   * @var Planet $planet
27
   */
28
  protected $planet;
29
30
  /**
31
   * Governor constructor.
32
   */
33
  public function __construct() {
34
    $this->reset();
35
  }
36
37
  /**
38
   * @param Planet $planet
39
   */
40
  public function setPlanet($planet) {
41
    $this->reset();
42
43
    $this->planet = $planet;
44
    $this->getPlanetData();
45
  }
46
47
  /**
48
   * @param $hireId
49
   */
50
  public function hire($hireId) {
51
    if (!in_array($hireId, sn_get_groups('governors'))) {
52
      return;
53
    }
54
55
    if ($hireId == $this->id && $this->getMaxLevel() && $this->getMaxLevel() >= $this->getLevel()) {
56
      return;
57
    }
58
59
    sn_db_transaction_start();
60
    $user = db_user_by_id($this->planet->id_owner, true);
0 ignored issues
show
Deprecated Code introduced by
The function db_user_by_id() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

60
    $user = /** @scrutinizer ignore-deprecated */ db_user_by_id($this->planet->id_owner, true);
Loading history...
Bug introduced by
It seems like $this->planet->id_owner can also be of type double; however, parameter $user_id_unsafe of db_user_by_id() does only seem to accept integer|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
    $user = db_user_by_id(/** @scrutinizer ignore-type */ $this->planet->id_owner, true);
Loading history...
61
//    $this->planetRow = DBStaticPlanet::db_planet_by_id($this->planet->id, true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
62
//    $build_data = eco_get_build_data($user, $this->planetRow, $hireId, $this->getId() == $hireId ? $this->getLevel() : 0);
63
    $this->planet->dbLoadRecord($this->planet->id);
0 ignored issues
show
Bug introduced by
It seems like $this->planet->id can also be of type string; however, parameter $id of Core\EntityDb::dbLoadRecord() does only seem to accept integer|double, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
    $this->planet->dbLoadRecord(/** @scrutinizer ignore-type */ $this->planet->id);
Loading history...
64
65
    $build_data = eco_get_build_data($user, $this->planet->_getContainer()->asArray(), $hireId, $this->getId() == $hireId ? $this->getLevel() : 0);
0 ignored issues
show
Bug introduced by
$user of type false is incompatible with the type array expected by parameter $user of eco_get_build_data(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
    $build_data = eco_get_build_data(/** @scrutinizer ignore-type */ $user, $this->planet->_getContainer()->asArray(), $hireId, $this->getId() == $hireId ? $this->getLevel() : 0);
Loading history...
66
    if (
67
      $build_data['CAN'][BUILD_CREATE]
68
      &&
69
      mrc_get_level($user, [], RES_DARK_MATTER) >= $build_data[BUILD_CREATE][RES_DARK_MATTER]
0 ignored issues
show
Bug introduced by
$user of type false is incompatible with the type array expected by parameter $user of mrc_get_level(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
      mrc_get_level(/** @scrutinizer ignore-type */ $user, [], RES_DARK_MATTER) >= $build_data[BUILD_CREATE][RES_DARK_MATTER]
Loading history...
70
      &&
71
      rpg_points_change(
72
        $user['id'],
73
        RPG_GOVERNOR,
74
        -$build_data[BUILD_CREATE][RES_DARK_MATTER],
75
        sprintf(classSupernova::$lang['ov_governor_purchase'],
76
          classSupernova::$lang['tech'][$hireId],
77
          $hireId,
78
          $this->level,
79
          uni_render_planet_object_full($this->planet, false, true)
80
        )
81
      )
82
    ) {
83
      $this->addLevel($hireId);
84
      $this->planet->_getContainer()->update();
85
//      DBStaticPlanet::db_planet_set_by_id($this->planet->id, "`{$this->typeIdField}` = {$this->id}, `{$this->typeLevelField}` = {$this->level}");
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
86
    }
87
    sn_db_transaction_commit();
88
  }
89
90
91
  /**
92
   * @return int
93
   */
94
  public function getId() {
95
    return $this->id;
96
  }
97
98
  /**
99
   * @return int
100
   */
101
  public function getLevel() {
102
    return $this->level;
103
  }
104
105
  /**
106
   * @return int
107
   */
108
  public function getMaxLevel() {
109
    return $this->id ? get_unit_param($this->id, P_MAX_STACK) : 0;
110
  }
111
112
  /**
113
   * @param $hireId
114
   */
115
  protected function addLevel($hireId) {
116
    if ($this->id == $hireId) {
117
      $this->level++;
118
    } else {
119
      $this->level = 1;
120
    }
121
122
    $this->id = $hireId;
123
124
    $this->setPlanetData();
125
  }
126
127
128
  protected function reset() {
129
    unset($this->unit);
130
    $this->planet = null;
131
132
    $this->id = 0;
133
    $this->level = 0;
134
  }
135
136
  protected function setPlanetData() {
137
    $this->planet->PLANET_GOVERNOR_ID = $this->id;
138
    $this->planet->PLANET_GOVERNOR_LEVEL = $this->level;
139
  }
140
141
  protected function getPlanetData() {
142
    $this->id = !empty($this->planet->PLANET_GOVERNOR_ID) ? intval($this->planet->PLANET_GOVERNOR_ID) : 0;
143
    $this->level = !empty($this->planet->PLANET_GOVERNOR_LEVEL) ? intval($this->planet->PLANET_GOVERNOR_LEVEL) : 0;
144
  }
145
146
}
147