Completed
Push — work-fleets ( 0f036f...867546 )
by SuperNova.WS
06:49
created

V2UnitContainer   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 153
Duplicated Lines 20.92 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 32
loc 153
ccs 0
cts 68
cp 0
rs 10
wmc 15
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 32 64 8
B isEmpty() 0 16 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    $that = $this;
104
105
    $this->assignAccessor('type', P_CONTAINER_SETTER,
106
      function ($value) use ($that) {
107
        $that->type = $value;
108
        $array = get_unit_param($value);
109
        $that->unitInfo = $array;
110
        // Mandatory
111
        $that->isStackable = empty($array[P_STACKABLE]) ? false : true;
112
        $that->locationDefaultType = empty($array[P_LOCATION_DEFAULT]) ? LOC_NONE : $array[P_LOCATION_DEFAULT];
113
        // Optional
114
        $that->bonusType = empty($array[P_BONUS_TYPE]) ? BONUS_NONE : $array[P_BONUS_TYPE];
115
      }
116
    );
117
118
    // This crap code is until php 5.4+. There we can use $this binding for lambdas
119
    $fieldName = $this->properties[$propertyName = 'timeStart'][P_DB_FIELD];
120
    $this->assignAccessor($propertyName, P_CONTAINER_IMPORTER,
121 View Code Duplication
      function (&$row) use ($that, $fieldName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
        if (isset($row[$fieldName])) {
123
          $dateTime = new \DateTime($row[$fieldName]);
124
        } else {
125
          $dateTime = null;
126
        }
127
        $that->timeStart = $dateTime;
128
      }
129
    );
130
    $this->assignAccessor($propertyName, P_CONTAINER_EXPORTER,
131 View Code Duplication
      function (&$row) use ($that, $fieldName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
        $dateTime = $that->timeStart;
133
        if ($dateTime instanceof \DateTime) {
134
          $row[$fieldName] = $dateTime->format(FMT_DATE_TIME_SQL);
135
        } else {
136
          $row[$fieldName] = null;
137
        }
138
      }
139
    );
140
141
    $fieldName = $this->properties[$propertyName = 'timeFinish'][P_DB_FIELD];
142
    $this->assignAccessor($propertyName, P_CONTAINER_IMPORTER,
143 View Code Duplication
      function (&$row) use ($that, $fieldName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
        if (isset($row[$fieldName])) {
145
          $dateTime = new \DateTime($row[$fieldName]);
146
        } else {
147
          $dateTime = null;
148
        }
149
        $that->timeFinish = $dateTime;
150
      }
151
    );
152
    $this->assignAccessor($propertyName, P_CONTAINER_EXPORTER,
153 View Code Duplication
      function (&$row) use ($that, $fieldName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
        $dateTime = $that->timeFinish;
155
        if ($dateTime instanceof \DateTime) {
156
          $row[$fieldName] = $dateTime->format(FMT_DATE_TIME_SQL);
157
        } else {
158
          $row[$fieldName] = null;
159
        }
160
      }
161
    );
162
163
  }
164
165
  public function isEmpty() {
166
    return
167
      empty($this->playerOwnerId)
168
      ||
169
      is_null($this->locationType)
170
      ||
171
      $this->locationType === LOC_NONE
172
      ||
173
      empty($this->locationId)
174
      ||
175
      empty($this->type)
176
      ||
177
      empty($this->snId)
178
      ||
179
      empty($this->level);
180
  }
181
182
}
183