Completed
Push — work-fleets ( 094cef...4ec5b3 )
by SuperNova.WS
06:12
created

V2FleetModel::loadById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 13
rs 9.4285
c 1
b 0
f 0
ccs 0
cts 8
cp 0
crap 6
1
<?php
2
/**
3
 * Created by Gorlum 17.08.2016 22:07
4
 */
5
6
namespace V2Fleet;
7
8
use V2Unit\V2UnitList;
9
use Vector\Vector;
10
11
/**
12
 * Class V2FleetModel
13
 *
14
 * @method V2FleetContainer getContainer()
15
 * @method V2FleetContainer fromArray(array $array)
16
 *
17
 * @package V2Fleet
18
 */
19
class V2FleetModel extends \EntityModel {
20
  /**
21
   * Name of table for this entity
22
   *
23
   * @var string $tableName
24
   */
25
  protected $tableName = 'fleets';
26
  /**
27
   * Name of key field field in this table
28
   *
29
   * @var string $idField
30
   */
31
  protected $idField = 'fleet_id';
32
33
//  protected $exceptionClass = '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...
34
  protected $entityContainerClass = 'V2Fleet\V2FleetContainer';
35
36
  protected $properties = array(
37
    'dbId'        => array(P_DB_FIELD => 'fleet_id',),
38
    'ownerId'     => array(P_DB_FIELD => 'fleet_owner',),
39
    'missionType' => array(P_DB_FIELD => 'fleet_mission'),
40
41
    'fleet_amount'          => array(P_DB_FIELD => 'fleet_amount'),
42
    'fleet_start_planet_id' => array(P_DB_FIELD => 'fleet_start_planet_id'),
43
44
//    'fleet_start_galaxy'       => array(P_DB_FIELD => 'fleet_start_galaxy'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% 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...
45
//    'fleet_start_system'       => array(P_DB_FIELD => 'fleet_start_system'),
46
//    'fleet_start_planet'       => array(P_DB_FIELD => 'fleet_start_planet'),
47
//    'fleet_start_type'         => array(P_DB_FIELD => 'fleet_start_type'),
48
49
    'fleet_end_planet_id' => array(P_DB_FIELD => 'fleet_end_planet_id'),
50
51
//    'fleet_end_galaxy'         => array(P_DB_FIELD => 'fleet_end_galaxy'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% 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...
52
//    'fleet_end_system'         => array(P_DB_FIELD => 'fleet_end_system'),
53
//    'fleet_end_planet'         => array(P_DB_FIELD => 'fleet_end_planet'),
54
//    'fleet_end_type'           => array(P_DB_FIELD => 'fleet_end_type'),
55
56
    'fleet_resource_metal'     => array(P_DB_FIELD => 'fleet_resource_metal'),
57
    'fleet_resource_crystal'   => array(P_DB_FIELD => 'fleet_resource_crystal'),
58
    'fleet_resource_deuterium' => array(P_DB_FIELD => 'fleet_resource_deuterium'),
59
    'fleet_target_owner'       => array(P_DB_FIELD => 'fleet_target_owner'),
60
    'fleet_group'              => array(P_DB_FIELD => 'fleet_group'),
61
    'fleet_mess'               => array(P_DB_FIELD => 'fleet_mess'),
62
63
    'vectorDeparture' => array(P_DB_FIELD => 'fleet_start_galaxy'),
64
    'vectorArrive'    => array(P_DB_FIELD => 'fleet_end_galaxy'),
65
66
    'timeDeparture' => array(P_DB_FIELD => 'start_time'),
67
    'timeArrive'    => array(P_DB_FIELD => 'fleet_start_time'),
68
    'timeComplete'  => array(P_DB_FIELD => 'fleet_end_stay'),
69
    'timeReturn'    => array(P_DB_FIELD => 'fleet_end_time'),
70
71
    'units' => array(),
72
  );
73
74
  public function __construct(\Common\GlobalContainer $gc) {
75
    parent::__construct($gc);
76
77
    $this->assignAccessor('vectorDeparture', P_CONTAINER_IMPORT, array($this, 'importVector'));
78
    $this->assignAccessor('vectorDeparture', P_CONTAINER_EXPORT, array($this, 'exportVector'));
79
    $this->assignAccessor('vectorArrive', P_CONTAINER_IMPORT, array($this, 'importVector'));
80
    $this->assignAccessor('vectorArrive', P_CONTAINER_EXPORT, array($this, 'exportVector'));
81
  }
82
83
  public function importVector(V2FleetContainer $that, array &$row, $propertyName, $fieldName) {
0 ignored issues
show
Unused Code introduced by
The parameter $fieldName 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...
84
    $prefix = $propertyName == 'vectorDeparture' ? 'fleet_start_' : 'fleet_end_';
85
    $that->$propertyName = new Vector(
86
      $row[$prefix . 'galaxy'],
87
      $row[$prefix . 'system'],
88
      $row[$prefix . 'planet'],
89
      $row[$prefix . 'type']
90
    );
91
  }
92
93
  public function exportVector(V2FleetContainer $that, array &$row, $propertyName, $fieldName) {
0 ignored issues
show
Unused Code introduced by
The parameter $fieldName 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...
94
    $prefix = $propertyName == 'vectorDeparture' ? 'fleet_start_' : 'fleet_end_';
95
    $row[$prefix . 'galaxy'] = $that->$propertyName->galaxy;
96
    $row[$prefix . 'system'] = $that->$propertyName->system;
97
    $row[$prefix . 'planet'] = $that->$propertyName->planet;
98
    $row[$prefix . 'type'] = $that->$propertyName->type;
99
  }
100
101
  /**
102
   * @param int|string $dbId
103
   *
104
   * @return V2FleetContainer|false
105
   */
106
  public function loadById($dbId) {
107
    if ($fleet = parent::loadById($dbId)) {
108
      /**
109
       * @var V2FleetContainer $fleet
110
       */
111
      // Loading units
112
      $units = new V2UnitList();
113
      $units->load(LOC_FLEET, $dbId);
114
      $fleet->units = $units;
0 ignored issues
show
Documentation introduced by
The property units does not exist on object<EntityContainer>. 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...
115
    }
116
117
    return $fleet;
118
  }
119
120
}
121