Completed
Push — work-fleets ( cef73c...bee521 )
by SuperNova.WS
06:54
created

EntityContainer::exportRowWithoutId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
use \Common\GlobalContainer;
4
5
/**
6
 * Class EntityContainer
7
 *
8
 * Support export/import accessors
9
 *
10
 * Importer is a callable like
11
 *    function ($that, &$row[, $propertyName[, $fieldName]]) {}
12
 *
13
 * Exporter is a callable like
14
 *    function ($that, &$row[, $propertyName[, $fieldName]]) {}
15
 *
16
 * @property int|float $dbId Entity DB ID
17
 */
18
class EntityContainer extends V2PropertyContainer implements IEntityContainer {
19
  const ENTITY_DB_ID_INCLUDE = true;
20
  const ENTITY_DB_ID_EXCLUDE = false;
21
22
  /**
23
   * @var EntityModel $model
24
   */
25
  protected $model;
26
  protected static $exceptionClass = 'EntityException';
27
  protected static $modelClass = 'EntityModel';
28
29
  /**
30
   * @var  \Common\GlobalContainer $gc
31
   */
32
  protected $gc;
33
  /**
34
   * Link to DB which used by this EntityModel
35
   *
36
   * @var \db_mysql $dbStatic
37
   * deprecated - replace with container ID like 'db' or 'dbAuth'
38
   */
39
  protected static $dbStatic;
40
  /**
41
   * Service to work with rows
42
   *
43
   * @var \DbRowDirectOperator $rowOperator
44
   */
45
  protected static $rowOperator;
46
  /**
47
   * Name of table for this entity
48
   *
49
   * @var string $tableName
50
   */
51
  protected $tableName = '_table';
52
  /**
53
   * Name of key field field in this table
54
   *
55
   * @var string $idField
56
   */
57
  protected $idField = 'id';
58
  /**
59
   * Property list and description
60
   *
61
   * propertyName => array(
62
   *    P_DB_FIELD => 'dbFieldName', - directly converts property to field and vice versa
63
   * )
64
   *
65
   * @var array[] $properties
66
   */
67
  protected $properties = array();
68
69
70
  /**
71
   * BuddyContainer constructor.
72
   *
73
   * @param GlobalContainer $gc
74
   */
75
  public function __construct($gc) {
76
    // TODO - remove. No dependenceon container - we should extract all needed info here
77
    $this->gc = $gc;
78
    $this->model = new static::$modelClass($gc);
79
    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...
80
    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...
81
  }
82
83
  /**
84
   * @return EntityModel
85
   */
86
  public function getModel() {
87
    return $this->model;
88
  }
89
90
  /**
91
   * @return \db_mysql
92
   */
93
  public function getDbStatic() {
94
    return static::$dbStatic;
95
  }
96
97
  public function setTableName($value) {
98
    $this->tableName = $value;
99
  }
100
101
  public function getTableName() {
102
    return $this->tableName;
103
  }
104
105
  public function setIdField($value) {
106
    $this->idField = $value;
107
  }
108
109
  public function getIdFieldName() {
110
    return $this->idField;
111
  }
112
113
  /**
114
   * @param array  $row
115
   * @param string $processor
116
   */
117
  protected function processRow(&$row, $processor) {
118
    foreach ($this->properties as $propertyName => $propertyData) {
119
      $fieldName = !empty($propertyData[P_DB_FIELD]) ? $propertyData[P_DB_FIELD] : '';
120
      if (
121
        !empty($this->accessors[$propertyName][$processor])
122
        &&
123
        is_callable($this->accessors[$propertyName][$processor])
124
      ) {
125
        call_user_func_array($this->accessors[$propertyName][$processor], array($this, &$row, $propertyName, $fieldName));
126
      } elseif ($fieldName) {
127
        if($processor == P_CONTAINER_IMPORTER) {
128
          $this->$propertyName = $row[$fieldName];
129
        } else {
130
          $row[$fieldName] = $this->$propertyName;
131
        }
132
      }
133
      // Otherwise it's internal field - filled and used internally
134
    }
135
136
  }
137
138
  public function importRow($row) {
139
    $this->clearProperties();
140
141
    if (empty($row)) {
142
      return true;
143
    }
144
145
    $this->processRow($row, P_CONTAINER_IMPORTER);
146
147
    return true;
148
  }
149
150
  /**
151
   * @return array
152
   */
153
  public function exportRow() {
154
    $row = array();
155
    $this->processRow($row, P_CONTAINER_EXPORTER);
156
157
    return $row;
158
  }
159
160
  /**
161
   * @return array
162
   */
163
  public function exportRowNoId() {
164
    $row = $this->exportRow();
165
166
    unset($row[$this->getIdFieldName()]);
167
168
    return $row;
169
  }
170
171
  // TODO - load from self DB
172
  public function loadTry() {
173
    $row = static::$rowOperator->getById($this);
174
175
    if (empty($row)) {
176
      $this->dbId = 0;
177
178
      return false;
179
    } else {
180
      $this->importRow($row);
181
    }
182
183
    return true;
184
  }
185
186
  public function isEmpty() {
187
    // TODO - empty container - only properties
188
    return empty($this->dbId);
189
  }
190
191
  public function isNew() {
192
    return empty($this->dbId);
193
  }
194
195
  public function insert() {
196
    static::$rowOperator->insert($this);
197
  }
198
199
  public function delete() {
200
    static::$rowOperator->deleteById($this);
201
  }
202
203
}
204