Completed
Push — trunk ( 8dcff2...338765 )
by SuperNova.WS
04:11
created

EntityDb::isDeleted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by Gorlum 08.01.2018 14:46
4
 */
5
6
namespace Core;
7
8
9
use \DBAL\DbQuery;
10
use \DBAL\ActiveRecord;
11
use Common\Traits\TContainer;
12
13
/**
14
 * Class EntityDb
15
 *
16
 * Represents in-game entity which have representation in DB (aka one or more connected ActiveRecords)
17
 *
18
 * @package Core
19
 *
20
 * @method array asArray() Extracts values as array [$propertyName => $propertyValue] (from ActiveRecord)
21
 * @method bool update() Updates DB record(s) in DB (from ActiveRecord)
22
 */
23
class EntityDb extends Entity implements \Common\Interfaces\IContainer {
24
  use TContainer;
25
26
  /**
27
   * @var string $_activeClass
28
   */
29
  protected $_activeClass = ''; // \\DBAL\\ActiveRecord
30
31
  /**
32
   * @var ActiveRecord $_container
33
   */
34
  protected $_container;
35
36
  protected $_isNew = true;
37
  protected $_isDeleted = false;
38
39
  /**
40
   * @return ActiveRecord
41
   */
42
  public function _getContainer() {
43
    return $this->_container;
44
  }
45
46
  /**
47
   * EntityDb constructor.
48
   */
49
  public function __construct() {
50
    $this->reset();
51
//    $this->dbLoadRecord($id);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
52
  }
53
54
  /**
55
   * Set flag "for update"
56
   *
57
   * @param bool $forUpdate - DbQuery::DB_FOR_UPDATE | DbQuery::DB_SHARED
58
   */
59
  public function setForUpdate($forUpdate = DbQuery::DB_FOR_UPDATE) {
60
    $className = $this->_activeClass;
61
    /**
62
     * @var ActiveRecord $className
63
     */
64
    $className::setForUpdate($forUpdate);
65
66
    return $this;
67
  }
68
69
  /**
70
   * @param int|float $id
71
   *
72
   * @return static
73
   */
74
  public function dbLoadRecord($id) {
75
    $this->reset();
76
77
    /**
78
     * @var ActiveRecord $className
79
     */
80
    $className = $this->_activeClass;
81
    $container = $className::findById($id);
82
    if(!empty($container)) {
83
      $this->_isNew = false;
84
      $this->_container = $container;
0 ignored issues
show
Documentation Bug introduced by
It seems like $container of type boolean is incompatible with the declared type DBAL\ActiveRecord of property $_container.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
85
    }
86
87
    return $this;
88
  }
89
90
  /**
91
   *
92
   */
93
  public function dbUpdate() {
94
    $this->_getContainer()->update();
95
  }
96
97
98
  public function isNew() {
99
    return $this->_isNew;
100
  }
101
102
  public function isDeleted() {
103
    return $this->_isDeleted;
104
  }
105
106
  public function reset() {
107
//    unset($this->_container);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
108
    $this->_container = new $this->_activeClass();
109
110
    $this->_isNew = true;
111
    $this->_isDeleted = false;
112
113
    return $this;
114
  }
115
116
}
117