Passed
Push — work-fleets ( 4e1f0c...91349a )
by SuperNova.WS
10:40
created

KeyedModel::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 9.4285
ccs 0
cts 6
cp 0
crap 2
1
<?php
2
/**
3
 * Created by Gorlum 19.08.2016 21:26
4
 */
5
6
namespace Entity;
7
8
/**
9
 * Class KeyedModel
10
 *
11
 * @method KeyedContainer fromArray($array)
12
 *
13
 * @package Entity
14
 */
15
16
class KeyedModel extends EntityModel {
17
  protected $newProperties = array(
18
    'dbId' => array(
19
      P_DB_FIELD => 'id',
20
    ),
21
  );
22
23
  public function __construct(\Common\GlobalContainer $gc) {
24
    parent::__construct($gc);
25
  }
26
27
  /**
28
   * Exports object properties to DB row state WITHOUT ID
29
   *
30
   * Useful for INSERT operations
31
   *
32
   * @param \Entity\KeyedContainer $cEntity
33
   */
34
  protected function exportRowNoId($cEntity) {
35
    $this->exportRow($cEntity);
36
37
    if (($idFieldName = $this->getIdFieldName()) != '') {
38
      unset($cEntity->row[$idFieldName]);
39
    }
40
  }
41
42
  /**
43
   * @param int|string $dbId
44
   *
45
   * @return KeyedContainer|false
46
   */
47
  public function loadById($dbId) {
48
    $row = $this->rowOperator->getById($this, $dbId);
49
    if (empty($row)) {
50
      return false;
51
    }
52
53
    $cEntity = $this->fromArray($row);
54
    $cEntity->isLoaded = true;
55
56
    return $cEntity;
57
  }
58
59
//  protected function load(KeyedContainer $cEntity) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% 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...
60
//    throw new \Exception('EntityModel::dbSave() is not yet implemented');
61
//  }
62
//
63
  protected function delete(KeyedContainer $cEntity) {
64
    $this->rowOperator->deleteById($this, $cEntity->dbId);
65
    $cEntity->isLoaded = false;
66
    $cEntity->isDeleted = true;
67
    throw new \Exception('KeyedModel::delete() is not yet implemented');
68
  }
69
70
  protected function insert(KeyedContainer $cEntity) {
71
    $cEntity->dbId = $this->rowOperator->insert($this, $this->exportRowNoId($cEntity));
0 ignored issues
show
Documentation introduced by
$this->exportRowNoId($cEntity) is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72
  }
73
74
  protected function update(KeyedContainer $cEntity) {
75
    // TODO - separate real changes from internal ones
76
    // Generate changeset row
77
    // Foreach all rows. If there is change and no delta - then put delta. Otherwise put change
78
    // If row not empty - update
79
    throw new \Exception('KeyedModel::update() is not yet implemented');
80
  }
81
82
  protected function unchanged(KeyedContainer $cEntity){
83
    // TODO - or just save nothing ?????
84
    // throw new \Exception('EntityModel isNotEmpty, have dbId and not CHANGED! It can\'t be!');
85
    // Do nothing
86
  }
87
88
  protected function emptyAction(KeyedContainer $cEntity) {
89
    // Just created container and doesn't use it
90
//    throw new \Exception('EntityModel isEmpty but not loaded! It can\'t be!');
91
    // Do nothing
92
  }
93
94
  /**
95
   * Gets entity's DB ID field name (which is unique within entity set)
96
   *
97
   * @return string
98
   */
99
  public function getIdFieldName() {
100
    return $this->properties['dbId'][P_DB_FIELD];
101
  }
102
103
}
104