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

DbRowDirectOperator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 13
Bugs 0 Features 1
Metric Value
dl 0
loc 86
rs 10
c 13
b 0
f 1
ccs 0
cts 39
cp 0
wmc 8
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getById() 0 9 1
A deleteById() 0 12 1
A insert() 0 11 2
A doSelectFetchValue() 0 3 1
A doSelectIterator() 0 3 1
A doUpdateRowSetAffected() 0 4 1
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