Completed
Push — work-fleets ( 4ec5b3...fe2ede )
by SuperNova.WS
10:06
created

V2UnitModel   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 14
Bugs 0 Features 2
Metric Value
dl 0
loc 197
rs 10
c 14
b 0
f 2
ccs 0
cts 95
cp 0
wmc 22
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 54 1
A setSnId() 0 17 4
A unsetSnId() 0 10 1
B validateCaptainVsUser() 0 14 7
B isEmpty() 0 16 7
A feature() 0 3 2
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';
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% 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...
35
  protected $entityContainerClass = 'V2Unit\V2UnitContainer';
36
37
//  protected $properties = array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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...
38
//    'dbId'                => array(
39
//      P_DB_FIELD => 'unit_id',
40
//    ),
41
//    'playerOwnerId'       => array(
42
//      P_DB_FIELD => 'unit_player_id',
43
//    ),
44
//    'locationType'        => array(
45
//      P_DB_FIELD => 'unit_location_type',
46
//    ),
47
//    'locationId'          => array(
48
//      P_DB_FIELD => 'unit_location_id',
49
//    ),
50
//    'type'                => array(
51
//      P_DB_FIELD => 'unit_type',
52
//    ),
53
//    'snId'                => array(
54
//      P_DB_FIELD => 'unit_snid',
55
//    ),
56
//    // Order is important!
57
//    // TODO - split dbLevel to level and count
58
//    'level'               => array(
59
//      P_DB_FIELD => 'unit_level',
60
//    ),
61
//    'count'               => array(),
62
//    // TODO - move to child class
63
//    'timeStart'           => array(
64
//      P_DB_FIELD => 'unit_time_start',
65
//    ),
66
//    'timeFinish'          => array(
67
//      P_DB_FIELD => 'unit_time_finish',
68
//    ),
69
//    // Do we need it? Or internal no info/getters/setters should be ignored?
70
//    'unitInfo'            => array(),
71
//    'isStackable'         => array(),
72
//    'locationDefaultType' => array(),
73
//    'bonusType'           => array(),
74
//  );
75
76
  public function __construct(\Common\GlobalContainer $gc) {
77
    parent::__construct($gc);
78
79
80
    $this->extendProperties(
81
      array(
82
        'playerOwnerId'       => array(
83
          P_DB_FIELD => 'unit_player_id',
84
        ),
85
        'locationType'        => array(
86
          P_DB_FIELD => 'unit_location_type',
87
        ),
88
        'locationId'          => array(
89
          P_DB_FIELD => 'unit_location_id',
90
        ),
91
        'type'                => array(
92
          P_DB_FIELD => 'unit_type',
93
        ),
94
        'snId'                => array(
95
          P_DB_FIELD => 'unit_snid',
96
        ),
97
        // Order is important!
98
        // TODO - split dbLevel to level and count
99
        'level'               => array(
100
          P_DB_FIELD => 'unit_level',
101
        ),
102
        'count'               => array(),
103
        // TODO - move to child class
104
        'timeStart'           => array(
105
          P_DB_FIELD => 'unit_time_start',
106
        ),
107
        'timeFinish'          => array(
108
          P_DB_FIELD => 'unit_time_finish',
109
        ),
110
        // Do we need it? Or internal no info/getters/setters should be ignored?
111
        'unitInfo'            => array(),
112
        'isStackable'         => array(),
113
        'locationDefaultType' => array(),
114
        'bonusType'           => array(),
115
      )
116
    );
117
118
    $this->accessors->setAccessor('snId', P_CONTAINER_SET, array($this, 'setSnId'));
119
    $this->accessors->setAccessor('snId', P_CONTAINER_UNSET, array($this, 'unsetSnId'));
120
121
    // This crap code is until php 5.4+. There we can use $this binding for lambdas
122
    $propertyName = 'timeStart';
123
    $this->accessors->setAccessor($propertyName, P_CONTAINER_IMPORT, array($gc->types, 'dateTimeImport'));
124
    $this->accessors->setAccessor($propertyName, P_CONTAINER_EXPORT, array($gc->types, 'dateTimeExport'));
125
126
    $propertyName = 'timeFinish';
127
    $this->accessors->setAccessor($propertyName, P_CONTAINER_IMPORT, array($gc->types, 'dateTimeImport'));
128
    $this->accessors->setAccessor($propertyName, P_CONTAINER_EXPORT, array($gc->types, 'dateTimeExport'));
129
  }
130
131
  public function setSnId(V2UnitContainer $that, $value) {
132
    $that->setDirect('snId', $value);
133
134
    $array = get_unit_param($value);
135
    $that->unitInfo = $array;
136
    $that->type = $array[P_UNIT_TYPE];
137
    // Mandatory
138
    $that->isStackable = empty($array[P_STACKABLE]) ? false : true;
139
    $that->locationDefaultType = empty($array[P_LOCATION_DEFAULT]) ? LOC_NONE : $array[P_LOCATION_DEFAULT];
140
    // Optional
141
    $that->bonusType = empty($array[P_BONUS_TYPE]) ? BONUS_NONE : $array[P_BONUS_TYPE];
142
    // TODO - Записывать перечень фич для модуля, определяемых по его типу
143
    // А фичи сначала должны быть где-то зарегестрированы - в каком-то сервис-локаторе
144
    // Что-то типа classSupernova::registerUnitFeature
145
    // Кэш фич для разных типов юнитов
146
    $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...
147
  }
148
149
  public function unsetSnId(V2UnitContainer $that) {
150
    unset($that->type);
151
    unset($that->unitInfo);
152
    // Mandatory
153
    unset($that->isStackable);
154
    unset($that->locationDefaultType);
155
    // Optional
156
    unset($that->bonusType);
157
    unset($that->features);
158
  }
159
160
  /**
161
   * @param V2UnitContainer $unitCaptain
162
   * @param int|string      $userId
163
   *
164
   * @throws \Entity\EntityException
165
   */
166
  // TODO - move to unitCaptain
167
  public function validateCaptainVsUser($unitCaptain, $userId) {
168
    if (!is_object($unitCaptain) || $this->isNew($unitCaptain) || $this->isEmpty($unitCaptain)) {
169
      throw new $this->$exceptionClass('module_unit_captain_error_not_found', ERR_ERROR);
170
    }
171
    if ($unitCaptain->snId != UNIT_CAPTAIN) {
172
      throw new $this->$exceptionClass('module_unit_captain_error_wrong_unit', ERR_ERROR);
173
    }
174
    if ($unitCaptain->playerOwnerId != $userId) {
175
      throw new $this->$exceptionClass('module_unit_captain_error_wrong_captain', ERR_ERROR);
176
    }
177
    if ($unitCaptain->locationType != LOC_PLANET) {
178
      throw new $this->$exceptionClass('module_unit_captain_error_wrong_location', ERR_ERROR);
179
    }
180
  }
181
182
  /**
183
   * @param V2UnitContainer $cUnit
184
   *
185
   * @return bool
186
   */
187
  public function isEmpty($cUnit) {
188
    return
189
      empty($cUnit->playerOwnerId)
190
      ||
191
      is_null($cUnit->locationType)
192
      ||
193
      $cUnit->locationType === LOC_NONE
194
      ||
195
      empty($cUnit->locationId)
196
      ||
197
      empty($cUnit->type)
198
      ||
199
      empty($cUnit->snId)
200
      ||
201
      empty($cUnit->level);
202
  }
203
204
  /**
205
   * @param V2UnitContainer $cUnit
206
   * @param string          $featureName
207
   *
208
   * return UnitFeature
209
   *
210
   * @return mixed|null
211
   */
212
  public function feature($cUnit, $featureName) {
213
    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...
214
  }
215
216
}
217