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

DbRowDirectOperator::__construct()   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 1
dl 0
loc 3
rs 10
c 2
b 0
f 0
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
3
/**
4
 * Class DbRowDirectOperator
5
 *
6
 * Handle Entity\EntityModel storing/loading operations
7
 */
8
class DbRowDirectOperator {
9
  protected $db;
10
11
  /**
12
   * DbRowDirectOperator constructor.
13
   *
14
   * @param db_mysql $db
15
   */
16
  public function __construct($db) {
17
    $this->db = $db;
18
  }
19
20
  /**
21
   * @param \Entity\KeyedModel $cModel
22
   * @param int|string          $dbId
23
   *
24
   * @return array
25
   */
26
  public function getById($cModel, $dbId) {
27
    $stmt = classSupernova::$gc->query
0 ignored issues
show
Bug introduced by
The method setIdField cannot be called on \classSupernova::$gc->query (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
28
      ->setIdField($cModel->getIdFieldName())
29
      ->field('*')
30
      ->from($cModel->getTableName())
31
      ->where($cModel->getIdFieldName() . ' = "' . $dbId . '"');
32
33
    return $stmt->selectRow();
34
  }
35
36
  /**
37
   * @param \Entity\KeyedModel $cModel
38
   * @param int|string          $dbId
39
   *
40
   * @return int
41
   */
42
  public function deleteById($cModel, $dbId) {
43
    $db = $this->db;
44
45
    $db->doDeleteRow(
46
      $cModel->getTableName(),
47
      array(
48
        $cModel->getIdFieldName() => $dbId,
49
      )
50
    );
51
52
    return $db->db_affected_rows();
53
  }
54
55
  /**
56
   * @param \Entity\EntityModel $cModel
57
   * @param array               $row
58
   *
59
   * @return int|string
60
   */
61
  public function insert($cModel, $row) {
62
    if (empty($row)) {
63
      // TODO Exception
64
      return 0;
65
    }
66
    $db = $this->db;
67
    $db->doInsertSet($cModel->getTableName(), $row);
68
69
    // TODO Exception if db_insert_id() is empty
70
    return $db->db_insert_id();
71
  }
72
73
  public function doSelectFetchValue($query) {
74
    return $this->db->doSelectFetchValue($query);
75
  }
76
77
  /**
78
   * Returns iterator to iterate through mysqli_result
79
   *
80
   * @param string $query
81
   *
82
   * @return DbEmptyIterator|DbMysqliResultIterator
83
   */
84
  public function doSelectIterator($query) {
85
    return $this->db->doSelectIterator($query);
86
  }
87
88
  public function doUpdateRowSetAffected($table, $fieldsAndValues, $where) {
89
    $this->db->doUpdateRowSet($table, $fieldsAndValues, $where);
90
    return $this->db->db_affected_rows();
91
  }
92
93
}
94