Completed
Push — work-fleets ( b30f48...88e4e3 )
by SuperNova.WS
08:13
created

KeyedModel::save()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 24
ccs 0
cts 16
cp 0
crap 20
rs 8.6845
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 save(KeyedContainer $cEntity) {
64
    if ($this->isEmpty($cEntity)) {
65
      if ($cEntity->isLoaded) {
66
        $this->rowOperator->deleteById($this, $cEntity->dbId);
67
      } else {
68
        throw new \Exception('EntityModel isEmpty but not loaded! It can\'t be!');
69
      }
70
    } else {
71
      if (!$cEntity->dbId) {
72
        $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...
73
//      } elseif ($this->isChanged($cEntity)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
74
//        // TODO - separate real changes from internal ones
75
//        // Generate changeset row
76
//        // Foreach all rows. If there is change and no delta - then put delta. Otherwise put change
77
//        // If row not empty - update
78
//        $this->update($cEntity);
79
      } else {
80
        // TODO - or just save nothing ?????
81
        throw new \Exception('EntityModel isNotEmpty, have dbId and not CHANGED! It can\'t be!');
82
      }
83
    }
84
85
    throw new \Exception('EntityModel::save() is not yet implemented');
86
  }
87
88
  /**
89
   * Gets entity's DB ID field name (which is unique within entity set)
90
   *
91
   * @return string
92
   */
93
  public function getIdFieldName() {
94
    return $this->properties['dbId'][P_DB_FIELD];
95
  }
96
97
}
98