Completed
Push — work-fleets ( 5c3a01...351cf4 )
by SuperNova.WS
06:40
created

V2UnitModel::unsetType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
ccs 0
cts 7
cp 0
crap 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 getContainer()
14
 *
15
 * @package V2Unit
16
 *
17
 */
18
19
class V2UnitModel extends \EntityModel {
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 $idField
30
   */
31
  protected $idField = 'unit_id';
32
33
  protected $exceptionClass = 'EntityException';
34
  protected $entityContainerClass = 'V2Unit\V2UnitContainer';
35
36
  protected $properties = array(
37
    'dbId'                => array(
38
      P_DB_FIELD => 'unit_id',
39
    ),
40
    'playerOwnerId'       => array(
41
      P_DB_FIELD => 'unit_player_id',
42
    ),
43
    'locationType'        => array(
44
      P_DB_FIELD => 'unit_location_type',
45
    ),
46
    'locationId'          => array(
47
      P_DB_FIELD => 'unit_location_id',
48
    ),
49
    'type'                => array(
50
      P_DB_FIELD => 'unit_type',
51
    ),
52
    'snId'                => array(
53
      P_DB_FIELD => 'unit_snid',
54
    ),
55
    // Order is important!
56
    // TODO - split dbLevel to level and count
57
    'level'               => array(
58
      P_DB_FIELD => 'unit_level',
59
    ),
60
    'count'               => array(),
61
    // TODO - move to child class
62
    'timeStart'           => array(
63
      P_DB_FIELD => 'unit_time_start',
64
    ),
65
    'timeFinish'          => array(
66
      P_DB_FIELD => 'unit_time_finish',
67
    ),
68
    // Do we need it? Or internal no info/getters/setters should be ignored?
69
    'unitInfo'            => array(),
70
    'isStackable'         => array(),
71
    'locationDefaultType' => array(),
72
    'bonusType'           => array(),
73
  );
74
75
  public function __construct(\Common\GlobalContainer $gc) {
76
    parent::__construct($gc);
77
78
    $this->assignAccessor('type', P_CONTAINER_SET, array($this, 'setType'));
79
    $this->assignAccessor('type', P_CONTAINER_UNSET, array($this, 'unsetType'));
80
81
    // This crap code is until php 5.4+. There we can use $this binding for lambdas
82
    $propertyName = 'timeStart';
83
    $this->assignAccessor($propertyName, P_CONTAINER_IMPORT, array($gc->types, 'dateTimeImport'));
84
    $this->assignAccessor($propertyName, P_CONTAINER_EXPORT, array($gc->types, 'dateTimeExport'));
85
86
    $propertyName = 'timeFinish';
87
    $this->assignAccessor($propertyName, P_CONTAINER_IMPORT, array($gc->types, 'dateTimeImport'));
88
    $this->assignAccessor($propertyName, P_CONTAINER_EXPORT, array($gc->types, 'dateTimeExport'));
89
  }
90
91
  public function setType(V2UnitContainer $that, $value) {
92
    $that->setDirect('type', $value);
93
    $array = get_unit_param($value);
94
    $that->unitInfo = $array;
95
    // Mandatory
96
    $that->isStackable = empty($array[P_STACKABLE]) ? false : true;
97
    $that->locationDefaultType = empty($array[P_LOCATION_DEFAULT]) ? LOC_NONE : $array[P_LOCATION_DEFAULT];
98
    // Optional
99
    $that->bonusType = empty($array[P_BONUS_TYPE]) ? BONUS_NONE : $array[P_BONUS_TYPE];
100
  }
101
102
  public function unsetType(V2UnitContainer $that, $value) {
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
    unset($that->type);
104
    unset($that->unitInfo);
105
    // Mandatory
106
    unset($that->isStackable);
107
    unset($that->locationDefaultType);
108
    // Optional
109
    unset($that->bonusType);
110
  }
111
112
  /**
113
   * @param V2UnitContainer $unitCaptain
114
   * @param int|string      $userId
115
   *
116
   * @throws \EntityException
117
   */
118
  // TODO - move to unitCaptain
119
  public function validateCaptainVsUser($unitCaptain, $userId) {
120
    if (!is_object($unitCaptain) || $unitCaptain->isNew() || $unitCaptain->isEmpty()) {
121
      throw new $this->$exceptionClass('module_unit_captain_error_not_found', ERR_ERROR);
122
    }
123
    if ($unitCaptain->snId != UNIT_CAPTAIN) {
124
      throw new $this->$exceptionClass('module_unit_captain_error_wrong_unit', ERR_ERROR);
125
    }
126
    if ($unitCaptain->playerOwnerId != $userId) {
127
      throw new $this->$exceptionClass('module_unit_captain_error_wrong_captain', ERR_ERROR);
128
    }
129
    if ($unitCaptain->locationType != LOC_PLANET) {
130
      throw new $this->$exceptionClass('module_unit_captain_error_wrong_location', ERR_ERROR);
131
    }
132
  }
133
134
}
135