Completed
Push — work-fleets ( d6880d...ad253d )
by SuperNova.WS
07:04
created

V2UnitModel::buildContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 2
rs 9.6666
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 fromArray(array $array)
14
 *
15
 * @package V2Unit
16
 *
17
 */
18
19
class V2UnitModel extends \Entity\KeyedModel {
20
  /**
21
   * Name of table for this entity
22
   *
23
   * @var string $tableName
24
   */
25
  protected $tableName = 'unit';
26
  /**
27
   * Name of key field field in this table
28
   *
29
   * @var string $idFieldName
30
   */
31
  protected $idFieldName = 'unit_id';
32
33
  protected $exceptionClass = 'Entity\EntityException';
34
  protected $entityContainerClass = 'V2Unit\V2UnitContainer';
35
36
  public function __construct(\Common\GlobalContainer $gc) {
37
    parent::__construct($gc);
38
39
    $this->extendProperties(
40
      array(
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
77
    $this->accessors->setAccessor('snId', P_CONTAINER_SET, array($this, 'setSnId'));
78
    $this->accessors->setAccessor('snId', P_CONTAINER_UNSET, array($this, 'unsetSnId'));
79
80
    // This crap code is until php 5.4+. There we can use $this binding for lambdas
81
    $propertyName = 'timeStart';
82
    $this->accessors->setAccessor($propertyName, P_CONTAINER_IMPORT, array($gc->types, 'dateTimeImport'));
83
    $this->accessors->setAccessor($propertyName, P_CONTAINER_EXPORT, array($gc->types, 'dateTimeExport'));
84
85
    $propertyName = 'timeFinish';
86
    $this->accessors->setAccessor($propertyName, P_CONTAINER_IMPORT, array($gc->types, 'dateTimeImport'));
87
    $this->accessors->setAccessor($propertyName, P_CONTAINER_EXPORT, array($gc->types, 'dateTimeExport'));
88
  }
89
90
  /**
91
   * @return V2UnitContainer
92
   */
93
  public function buildContainer($snId = 0, $level = 0) {
94
    /**
95
     * @var V2UnitContainer $unit
96
     */
97
    $unit = parent::buildContainer();
98
    $unit->snId = $snId;
99
    $unit->level = $level;
100
    return $unit;
101
  }
102
103
  public function setSnId(V2UnitContainer $that, $value) {
104
    $that->setDirect('snId', $value);
105
106
    $array = get_unit_param($value);
107
    $that->unitInfo = $array;
108
    $that->type = $array[P_UNIT_TYPE];
109
    // Mandatory
110
    $that->isStackable = empty($array[P_STACKABLE]) ? false : true;
111
    $that->locationDefaultType = empty($array[P_LOCATION_DEFAULT]) ? LOC_NONE : $array[P_LOCATION_DEFAULT];
112
    // Optional
113
    $that->bonusType = empty($array[P_BONUS_TYPE]) ? BONUS_NONE : $array[P_BONUS_TYPE];
114
    // TODO - Записывать перечень фич для модуля, определяемых по его типу
115
    // А фичи сначала должны быть где-то зарегестрированы - в каком-то сервис-локаторе
116
    // Что-то типа classSupernova::registerUnitFeature
117
    // Кэш фич для разных типов юнитов
118
    $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...
119
  }
120
121
  public function unsetSnId(V2UnitContainer $that) {
122
    unset($that->type);
123
    unset($that->unitInfo);
124
    // Mandatory
125
    unset($that->isStackable);
126
    unset($that->locationDefaultType);
127
    // Optional
128
    unset($that->bonusType);
129
    unset($that->features);
130
  }
131
132
  /**
133
   * @param V2UnitContainer $unitCaptain
134
   * @param int|string      $userId
135
   *
136
   * @throws \Entity\EntityException
137
   */
138
  // TODO - move to unitCaptain
139
  public function validateCaptainVsUser($unitCaptain, $userId) {
140
    if (!is_object($unitCaptain) || $this->isNew($unitCaptain) || $this->isEmpty($unitCaptain)) {
141
      throw new $this->$exceptionClass('module_unit_captain_error_not_found', ERR_ERROR);
142
    }
143
    if ($unitCaptain->snId != UNIT_CAPTAIN) {
144
      throw new $this->$exceptionClass('module_unit_captain_error_wrong_unit', ERR_ERROR);
145
    }
146
    if ($unitCaptain->playerOwnerId != $userId) {
147
      throw new $this->$exceptionClass('module_unit_captain_error_wrong_captain', ERR_ERROR);
148
    }
149
    if ($unitCaptain->locationType != LOC_PLANET) {
150
      throw new $this->$exceptionClass('module_unit_captain_error_wrong_location', ERR_ERROR);
151
    }
152
  }
153
154
  /**
155
   * @param V2UnitContainer $cUnit
156
   *
157
   * @return bool
158
   */
159
  public function isEmpty($cUnit) {
160
    return
161
      empty($cUnit->playerOwnerId)
162
      ||
163
      is_null($cUnit->locationType)
164
      ||
165
      $cUnit->locationType === LOC_NONE
166
      ||
167
      empty($cUnit->locationId)
168
      ||
169
      empty($cUnit->type)
170
      ||
171
      empty($cUnit->snId)
172
      ||
173
      empty($cUnit->level);
174
  }
175
176
  /**
177
   * @param V2UnitContainer $cUnit
178
   * @param string          $featureName
179
   *
180
   * return UnitFeature
181
   *
182
   * @return mixed|null
183
   */
184
  public function feature($cUnit, $featureName) {
185
    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...
186
  }
187
188
}
189