Completed
Push — work-fleets ( bee521...bd15ac )
by SuperNova.WS
07:12
created

V2UnitContainer::__construct()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 6
Bugs 1 Features 0
Metric Value
cc 4
eloc 16
c 6
b 1
f 0
nc 1
nop 1
dl 0
loc 25
rs 8.5806
ccs 0
cts 19
cp 0
crap 20
1
<?php
2
/**
3
 * Created by Gorlum 10.08.2016 14:25
4
 */
5
6
namespace V2Unit;
7
use Common\GlobalContainer;
8
9
/**
10
 * Class V2UnitContainer
11
 *
12
 * @method V2UnitModel getModel()
13
 *
14
 * @property int       $playerOwnerId
15
 * @property int       $locationType
16
 * @property int       $locationId
17
 * @property int       $type
18
 * @property int       $snId
19
 * @property int       $level - level of unit for DB: $count for stackable units, $level - fon unstackable units
20
 * property int $count // TODO
21
 * @property \DateTime $timeStart
22
 * @property \DateTime $timeFinish
23
 * @property bool      $isStackable
24
 * @property string    $locationDefaultType
25
 * @property int       $bonusType // TODO - Optional?
26
 * @property array     $unitInfo - full info about unit
27
 *
28
 * @package V2Unit
29
 */
30
class V2UnitContainer extends \EntityContainer {
31
  /**
32
   * @var V2UnitModel $model
33
   */
34
  protected $model;
35
36
  protected static $exceptionClass = 'EntityException';
37
  protected static $modelClass = 'V2Unit\V2UnitModel';
38
39
  /**
40
   * Name of table for this entity
41
   *
42
   * @var string $tableName
43
   */
44
  protected $tableName = 'unit';
45
  /**
46
   * Name of key field field in this table
47
   *
48
   * @var string $idField
49
   */
50
  protected $idField = 'unit_id';
51
  /**
52
   * Property list
53
   *
54
   * @var array $properties
55
   */
56
  protected $properties = array(
57
    'dbId'                => array(
58
      P_DB_FIELD => 'unit_id',
59
    ),
60
    'playerOwnerId'       => array(
61
      P_DB_FIELD => 'unit_player_id',
62
    ),
63
    'locationType'        => array(
64
      P_DB_FIELD => 'unit_location_type',
65
    ),
66
    'locationId'          => array(
67
      P_DB_FIELD => 'unit_location_id',
68
    ),
69
    'type'                => array(
70
      P_DB_FIELD => 'unit_type',
71
    ),
72
    'snId'                => array(
73
      P_DB_FIELD => 'unit_snid',
74
    ),
75
    // Order is important!
76
    // TODO - split dbLevel to level and count
77
    'level'               => array(
78
      P_DB_FIELD => 'unit_level',
79
    ),
80
    'count'               => array(),
81
    // TODO - move to child class
82
    'timeStart'           => array(
83
      P_DB_FIELD => 'unit_time_start',
84
    ),
85
    'timeFinish'          => array(
86
      P_DB_FIELD => 'unit_time_finish',
87
    ),
88
    // Do we need it? Or internal no info/getters/setters should be ignored?
89
    'unitInfo'            => array(),
90
    'isStackable'         => array(),
91
    'locationDefaultType' => array(),
92
    'bonusType'           => array(),
93
  );
94
95
  /**
96
   * BuddyContainer constructor.
97
   *
98
   * @param GlobalContainer $gc
99
   */
100
  public function __construct($gc) {
101
    parent::__construct($gc);
102
103
    $this->assignAccessor('type', P_CONTAINER_SET,
104
      function (V2UnitContainer $that, $value) {
105
        $that->setDirect('type', $value);
106
        $array = get_unit_param($value);
107
        $that->unitInfo = $array;
108
        // Mandatory
109
        $that->isStackable = empty($array[P_STACKABLE]) ? false : true;
110
        $that->locationDefaultType = empty($array[P_LOCATION_DEFAULT]) ? LOC_NONE : $array[P_LOCATION_DEFAULT];
111
        // Optional
112
        $that->bonusType = empty($array[P_BONUS_TYPE]) ? BONUS_NONE : $array[P_BONUS_TYPE];
113
      }
114
    );
115
116
    // This crap code is until php 5.4+. There we can use $this binding for lambdas
117
    $propertyName = 'timeStart';
118
    $this->assignAccessor($propertyName, P_CONTAINER_IMPORT, array($gc->types, 'dateTimeImport'));
119
    $this->assignAccessor($propertyName, P_CONTAINER_EXPORT, array($gc->types, 'dateTimeExport'));
120
121
    $propertyName = 'timeFinish';
122
    $this->assignAccessor($propertyName, P_CONTAINER_IMPORT, array($gc->types, 'dateTimeImport'));
123
    $this->assignAccessor($propertyName, P_CONTAINER_EXPORT, array($gc->types, 'dateTimeExport'));
124
  }
125
126
  public function isEmpty() {
127
    return
128
      empty($this->playerOwnerId)
129
      ||
130
      is_null($this->locationType)
131
      ||
132
      $this->locationType === LOC_NONE
133
      ||
134
      empty($this->locationId)
135
      ||
136
      empty($this->type)
137
      ||
138
      empty($this->snId)
139
      ||
140
      empty($this->level);
141
  }
142
143
}
144