Completed
Push — work-fleets ( bd15ac...5c3a01 )
by SuperNova.WS
06:16
created

EntityModel::loadTry()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 1
b 0
f 0
ccs 0
cts 9
cp 0
crap 6
1
<?php
2
3
/**
4
 * Class EntityModel
5
 *
6
 * @property int|float $dbId Buddy record DB ID
7
 */
8
class EntityModel implements \Common\IEntityModel {
9
  /**
10
   * Link to DB which used by this EntityModel
11
   *
12
   * @var \db_mysql $dbStatic
13
   * deprecated - replace with container ID like 'db' or 'dbAuth'
14
   */
15
  protected static $dbStatic;
16
  /**
17
   * Service to work with rows
18
   *
19
   * @var \DbRowDirectOperator $rowOperator
20
   */
21
  protected static $rowOperator;
22
23
  /**
24
   * Name of table for this entity
25
   *
26
   * @var string $tableName
27
   */
28
  protected $tableName = '_table';
29
30
  /**
31
   * Name of key field field in this table
32
   *
33
   * @var string $idField
34
   */
35
  protected $idField = 'id';
36
//  /**
37
//   * Property list and description
38
//   *
39
//   * propertyName => array(
40
//   *    P_DB_FIELD => 'dbFieldName', - directly converts property to field and vice versa
41
//   * )
42
//   *
43
//   * @var array[] $properties
44
//   */
45
//  protected $properties = array();
46
47
  /**
48
   * Name of exception class that would be thrown
49
   *
50
   * Uses for calling when you don't know which exact exception should be called
51
   * On EntityModel's children should be used exception class name
52
   *
53
   * @var string $exceptionClass
54
   */
55
  protected static $exceptionClass = 'EntityException';
56
  protected static $entityContainerClass = '\EntityContainer';
57
58
59
  /**
60
   * EntityModel constructor.
61
   *
62
   * @param \Common\GlobalContainer $gc
63
   */
64
  public function __construct($gc) {
65
    static::$dbStatic = $gc->db;
0 ignored issues
show
Documentation Bug introduced by
It seems like $gc->db can also be of type object<Closure>. However, the property $dbStatic is declared as type object<db_mysql>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
66
    static::$rowOperator = $gc->dbRowOperator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $gc->dbRowOperator can also be of type object<Closure>. However, the property $rowOperator is declared as type object<DbRowDirectOperator>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
67
  }
68
69
  /**
70
   * @return \db_mysql
71
   */
72
  public function getDbStatic() {
73
    return static::$dbStatic;
74
  }
75
76
  /**
77
   * @return \DbRowDirectOperator
78
   */
79
  public function getRowOperator() {
80
    return static::$rowOperator;
81
  }
82
83
  public function setTableName($value) {
84
    $this->tableName = $value;
85
  }
86
87
  public function getTableName() {
88
    return $this->tableName;
89
  }
90
91
  public function setIdFieldName($value) {
92
    $this->idField = $value;
93
  }
94
95
  public function getIdFieldName() {
96
    return $this->idField;
97
  }
98
99
100
101
102
103
  public function fromArray($array) {
104
    /**
105
     * @var EntityContainer $cEntity
106
     */
107
    $cEntity = new static::$entityContainerClass(classSupernova::$gc);
108
    $cEntity->importRow($array);
109
110
    return $cEntity;
111
  }
112
113
114
  public function exportRow($cEntity) {
115
    return $cEntity->exportRow();
116
  }
117
118
  public function exportRowNoId($cEntity) {
119
    $row = $this->exportRow($cEntity);
120
121
    unset($row[$this->getIdFieldName()]);
122
123
    return $row;
124
  }
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  public function loadTry($dbId) {
139
    $row = static::$rowOperator->getById($this, $dbId);
140
    if (empty($row)) {
141
      return false;
142
    } else {
143
      $cEntity = $this->fromArray($row);
144
    }
145
146
    return $cEntity;
147
  }
148
149
}
150