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

V2UnitModel::setSnId()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 5
eloc 12
c 3
b 0
f 1
nc 9
nop 2
dl 0
loc 21
rs 8.7624
ccs 0
cts 14
cp 0
crap 30
1
<?php
2
/**
3
 * Created by Gorlum 29.07.2016 13:18
4
 */
5
6
namespace V2Unit;
7
8
/**
9
 * Class V2UnitModel
10
 *
11
 * Second iteration of revised Unit
12
 *
13
 * @method V2UnitContainer buildContainer()
14
 * @method V2UnitContainer fromArray(array $array)
15
 *
16
 * @package V2Unit
17
 *
18
 */
19
20
class V2UnitModel extends \Entity\KeyedModel {
21
  /**
22
   * Name of table for this entity
23
   *
24
   * @var string $tableName
25
   */
26
  protected $tableName = 'unit';
27
  /**
28
   * Name of key field field in this table
29
   *
30
   * @var string $idFieldName
31
   */
32
  protected $idFieldName = 'unit_id';
33
34
  protected $exceptionClass = 'Entity\EntityException';
35
  protected $entityContainerClass = 'V2Unit\V2UnitContainer';
36
37
  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...
38
    'playerOwnerId'       => array(
39
      P_DB_FIELD => 'unit_player_id',
40
    ),
41
    'locationType'        => array(
42
      P_DB_FIELD => 'unit_location_type',
43
    ),
44
    'locationId'          => array(
45
      P_DB_FIELD => 'unit_location_id',
46
    ),
47
    'type'                => array(
48
      P_DB_FIELD => 'unit_type',
49
    ),
50
    'snId'                => array(
51
      P_DB_FIELD => 'unit_snid',
52
    ),
53
    // Order is important!
54
    // TODO - split dbLevel to level and count
55
    'level'               => array(
56
      P_DB_FIELD => 'unit_level',
57
    ),
58
    'count'               => array(),
59
    // TODO - move to child class
60
    'timeStart'           => array(
61
      P_DB_FIELD => 'unit_time_start',
62
    ),
63
    'timeFinish'          => array(
64
      P_DB_FIELD => 'unit_time_finish',
65
    ),
66
    // Do we need it? Or internal no info/getters/setters should be ignored?
67
    'unitInfo'            => array(),
68
    'isStackable'         => array(),
69
    'locationDefaultType' => array(),
70
    'bonusType'           => array(),
71
  );
72
73
  public function __construct(\Common\GlobalContainer $gc) {
74
    parent::__construct($gc);
75
76
    $this->accessors->set('snId', P_CONTAINER_SET, array($this, 'setSnId'));
77
    $this->accessors->set('snId', P_CONTAINER_UNSET, array($this, 'unsetSnId'));
78
79
    // This crap code is until php 5.4+. There we can use $this binding for lambdas
80
    $propertyName = 'timeStart';
81
    $this->accessors->set($propertyName, P_CONTAINER_IMPORT, array($gc->types, 'dateTimeImport'));
82
    $this->accessors->set($propertyName, P_CONTAINER_EXPORT, array($gc->types, 'dateTimeExport'));
83
84
    $propertyName = 'timeFinish';
85
    $this->accessors->set($propertyName, P_CONTAINER_IMPORT, array($gc->types, 'dateTimeImport'));
86
    $this->accessors->set($propertyName, P_CONTAINER_EXPORT, array($gc->types, 'dateTimeExport'));
87
  }
88
89
  public function setSnId(V2UnitContainer $that, $value) {
90
    $value = intval($value);
91
    $that->setDirect('snId', $value);
92
    if (empty($value)) {
93
      return;
94
    }
95
96
    $array = get_unit_param($value);
97
    $that->unitInfo = $array;
98
    $that->type = $array[P_UNIT_TYPE];
99
    // Mandatory
100
    $that->isStackable = empty($array[P_STACKABLE]) ? false : true;
101
    $that->locationDefaultType = empty($array[P_LOCATION_DEFAULT]) ? LOC_NONE : $array[P_LOCATION_DEFAULT];
102
    // Optional
103
    $that->bonusType = empty($array[P_BONUS_TYPE]) ? BONUS_NONE : $array[P_BONUS_TYPE];
104
    // TODO - Записывать перечень фич для модуля, определяемых по его типу
105
    // А фичи сначала должны быть где-то зарегестрированы - в каком-то сервис-локаторе
106
    // Что-то типа classSupernova::registerUnitFeature
107
    // Кэш фич для разных типов юнитов
108
    $that->features = array(); //new FeatureList($that->unitInfo['features']);
0 ignored issues
show
Documentation introduced by
The property features does not exist on object<V2Unit\V2UnitContainer>. 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...
Unused Code Comprehensibility introduced by
75% 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...
109
  }
110
111
  public function unsetSnId(V2UnitContainer $that) {
112
    unset($that->type);
113
    unset($that->unitInfo);
114
    // Mandatory
115
    unset($that->isStackable);
116
    unset($that->locationDefaultType);
117
    // Optional
118
    unset($that->bonusType);
119
    unset($that->features);
120
  }
121
122
  /**
123
   * @param V2UnitContainer $unitCaptain
124
   * @param int|string      $userId
125
   *
126
   * @throws \Entity\EntityException
127
   */
128
  // TODO - move to unitCaptain
129
  public function validateCaptainVsUser($unitCaptain, $userId) {
130
    if (!is_object($unitCaptain) || $this->isNew($unitCaptain) || $this->isEmpty($unitCaptain)) {
131
      throw new $this->$exceptionClass('module_unit_captain_error_not_found', ERR_ERROR);
132
    }
133
    if ($unitCaptain->snId != UNIT_CAPTAIN) {
134
      throw new $this->$exceptionClass('module_unit_captain_error_wrong_unit', ERR_ERROR);
135
    }
136
    if ($unitCaptain->playerOwnerId != $userId) {
137
      throw new $this->$exceptionClass('module_unit_captain_error_wrong_captain', ERR_ERROR);
138
    }
139
    if ($unitCaptain->locationType != LOC_PLANET) {
140
      throw new $this->$exceptionClass('module_unit_captain_error_wrong_location', ERR_ERROR);
141
    }
142
  }
143
144
  /**
145
   * @param V2UnitContainer $cUnit
146
   *
147
   * @return bool
148
   */
149
  public function isEmpty($cUnit) {
150
    return
151
      empty($cUnit->playerOwnerId)
152
      ||
153
      is_null($cUnit->locationType)
154
      ||
155
      $cUnit->locationType === LOC_NONE
156
      ||
157
      empty($cUnit->locationId)
158
      ||
159
      empty($cUnit->type)
160
      ||
161
      empty($cUnit->snId)
162
      ||
163
      empty($cUnit->level);
164
  }
165
166
  /**
167
   * @param V2UnitContainer $cUnit
168
   * @param string          $featureName
169
   *
170
   * return UnitFeature
171
   *
172
   * @return mixed|null
173
   */
174
  public function feature($cUnit, $featureName) {
175
    return isset($cUnit->features[$featureName]) ? $cUnit->features[$featureName] : null;
0 ignored issues
show
Documentation introduced by
The property features does not exist on object<V2Unit\V2UnitContainer>. 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...
176
  }
177
178
}
179