Completed
Push — work-fleets ( 4ec5b3...fe2ede )
by SuperNova.WS
10:06
created

EntityModel::getAccessors()   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 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 2
b 0
f 0
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
namespace Entity;
3
4
/**
5
 * Class Entity\EntityModel
6
 *
7
 * This class have only one instance - i.e. is a service
8
 * Describes persistent entity - which can be loaded from/stored to storage
9
 *
10
 *
11
 * Introduces linked models and export/import operations
12
 *
13
 * Importer is a callable like
14
 *    function ($that, &$row[, $propertyName[, $fieldName]]) {}
15
 *
16
 * Exporter is a callable like
17
 *    function ($that, &$row[, $propertyName[, $fieldName]]) {}
18
 *
19
 */
20
21
class EntityModel {
22
  /**
23
   * Service to work with rows
24
   *
25
   * ALL DB ACCESS SHOULD BE DONE VIA ROW OPERATOR! NO DIRECT ACCESS TO DB IS ALLOWED!
26
   *
27
   * @var \DbRowDirectOperator $rowOperator
28
   */
29
  protected $rowOperator;
30
  /**
31
   * Name of table for this entity
32
   *
33
   * @var string $tableName
34
   */
35
  protected $tableName = '_table';
36
37
  /**
38
   * Name of key field field in this table
39
   *
40
   * @var string $idFieldName
41
   */
42
  // TODO - remove
43
  protected $idFieldName = 'id';
44
45
  /**
46
   * Name of exception class that would be thrown
47
   *
48
   * Uses for calling when you don't know which exact exception should be called
49
   * On Entity\EntityModel's children should be used exception class name
50
   *
51
   * @var string $exceptionClass
52
   */
53
  protected $exceptionClass = 'Entity\EntityException';
54
  protected $entityContainerClass = '\Entity\EntityContainer';
55
56
  /**
57
   * Property list and description
58
   *
59
   * propertyName => array(
60
   *    P_DB_FIELD => 'dbFieldName', - directly converts property to field and vice versa
61
   * )
62
   *
63
   * @var array[] $properties
64
   */
65
  protected $properties = array();
66
67
  /**
68
   * @var \Common\Accessors $accessors
69
   */
70
  protected $accessors;
71
72
73
  /**
74
   * Entity\EntityModel constructor.
75
   *
76
   * @param \Common\GlobalContainer $gc
77
   */
78
  public function __construct($gc) {
79
    // Here own rowOperator can be made - if needed to operate other, non-global, DB
80
    $this->rowOperator = $gc->dbGlobalRowOperator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $gc->dbGlobalRowOperator 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
    $this->accessors = new \Common\Accessors();
82
  }
83
84
  /**
85
   * @param EntityContainer $that
86
   * @param string                  $accessor
87
   */
88
  protected function processRow($that, $accessor) {
89
    foreach ($this->properties as $propertyName => $propertyData) {
90
      $fieldName = !empty($propertyData[P_DB_FIELD]) ? $propertyData[P_DB_FIELD] : '';
91
      if ($this->accessors->haveAccessor($propertyName, $accessor)) {
92
        $this->accessors->invokeAccessor($propertyName, $accessor, array($that, $propertyName, $fieldName));
93
      } elseif ($fieldName) {
94
        if ($accessor == P_CONTAINER_IMPORT) {
95
          $that->$propertyName = isset($that->row[$fieldName]) ? $that->row[$fieldName] : null;
96
        } else {
97
          $that->row += array($fieldName => $that->$propertyName);
98
        }
99
      }
100
      // Otherwise it's internal field - filled and used internally
101
    }
102
  }
103
104
  /**
105
   * Import DB row state into object properties
106
   *
107
   * @param EntityContainer $cEntity
108
   * @param array           $row
109
   */
110
  public function importRow($cEntity, $row) {
111
    $cEntity->clear();
112
    $cEntity->row = $row;
113
114
    if (is_array($row) && !empty($row)) {
115
      $this->processRow($cEntity, P_CONTAINER_IMPORT);
116
    }
117
  }
118
119
  /**
120
   * @param array $array
121
   *
122
   * @return EntityContainer
123
   */
124
  public function fromArray($array) {
125
    /**
126
     * @var EntityContainer $cEntity
127
     */
128
    $cEntity = $this->buildContainer();
129
    $this->importRow($cEntity, $array);
130
131
    return $cEntity;
132
  }
133
134
  /**
135
   * Exports object properties to DB row state with ID
136
   *
137
   * @param EntityContainer $cEntity
138
   */
139
  public function exportRow($cEntity) {
140
    $cEntity->row = array();
141
    $this->processRow($cEntity, P_CONTAINER_EXPORT);
142
  }
143
144
  /**
145
   * @return EntityContainer
146
   */
147
  public function buildContainer() {
148
    /**
149
     * @var EntityContainer $container
150
     */
151
    $container = new $this->entityContainerClass($this);
152
153
    return $container;
154
  }
155
156
157
  /**
158
   * @param EntityContainer $cEntity
159
   *
160
   * @return bool
161
   */
162
  public function isEmpty($cEntity) {
163
    return $cEntity->isEmpty();
164
  }
165
166
  /**
167
   * @param EntityContainer $cEntity
168
   *
169
   * @return bool
170
   */
171
  // TODO - Loaded flag ?????
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% 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...
172
  public function isNew($cEntity) {
173
    return $cEntity->isEmpty();
174
  }
175
176
177
//  /**
178
//   * Clears only properties which declared in $properties array
179
//   *
180
//   * @param EntityContainer $cEntity
181
//   */
182
//  public function clearProperties($cEntity) {
183
//    foreach ($this->properties as $propertyName => $propertyData) {
184
//      unset($cEntity->$propertyName);
185
//    }
186
//  }
187
188
189
  /**
190
   * @return \DbRowDirectOperator
191
   */
192
  public function getRowOperator() {
193
    return $this->rowOperator;
194
  }
195
196
  /**
197
   * @param string $value
198
   */
199
  public function setTableName($value) {
200
    $this->tableName = $value;
201
  }
202
203
  /**
204
   * Gets entity's table name
205
   *
206
   * @return string
207
   */
208
  public function getTableName() {
209
    return $this->tableName;
210
  }
211
212
//  /**
213
//   * @param string $value
214
//   */
215
//  public function setIdFieldName($value) {
216
//    $this->idField = $value;
217
//  }
218
//  /**
219
//   * Gets entity's DB ID field name (which is unique within entity set)
220
//   *
221
//   * @return string
222
//   */
223
//  public function getIdFieldName() {
224
//    return $this->idField;
225
//  }
226
227
  /**
228
   * @return array[]
229
   */
230
  public function getProperties() {
231
    return $this->properties;
232
  }
233
234
  /**
235
   * @param $array
236
   */
237
  public function extendProperties($array) {
238
    $this->properties += $array;
239
  }
240
241
  /**
242
   * @return \Common\Accessors
243
   */
244
  public function getAccessors() {
245
    return $this->accessors;
246
  }
247
248
}
249