Completed
Push — work-fleets ( addb18...c7cb6b )
by SuperNova.WS
06:04
created

DbRowDirectOperator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 54
rs 10
c 1
b 0
f 0
ccs 0
cts 29
cp 0
wmc 6
lcom 0
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getById() 0 9 1
A deleteById() 0 7 1
B insert() 0 23 4
1
<?php
2
3
/**
4
 * Class DbRowDirectOperator
5
 *
6
 * Handle Entity storing/loading operations
7
 */
8
9
class DbRowDirectOperator implements \Common\IEntityOperator {
10
11
  /**
12
   * @param Entity $entity
13
   */
14
  public function getById($entity) {
15
    $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...
16
      ->setIdField($entity->getIdFieldName())
17
      ->field('*')
18
      ->from($entity->getTableName())
19
      ->where($entity->getIdFieldName() . ' = "' . $entity->dbId . '"');
20
21
    $entity->importDbRow($stmt->selectRow());
22
  }
23
24
  /**
25
   * @param Entity $entity
26
   */
27
  public function deleteById($entity) {
28
    $db = $entity->getDbStatic();
29
30
    $db->doquery("DELETE FROM `{{" . $entity->getTableName() . "}}` WHERE `{$entity->getIdFieldName()}` = '{$entity->dbId}' LIMIT 1;");
31
32
    return $db->db_affected_rows();
33
  }
34
35
  /**
36
   * @param Entity $entity
37
   */
38
  public function insert($entity) {
39
    $db = $entity->getDbStatic();
40
41
    $query = array();
42
    foreach ($entity->exportRowWithoutId() as $fieldName => $fieldValue) {
43
      // TODO: MORE type detection
44
      if(!is_numeric($fieldValue)) {
45
        $fieldValue = "'" . $db->db_escape($fieldValue) . "'";
46
      }
47
      $query[] = "`{$fieldName}` = {$fieldValue}";
48
    }
49
50
    $query = implode(',', $query);
51
    if (empty($query)) {
52
      // TODO Exceptiion
53
      return 0;
54
    }
55
56
    $db->doquery("INSERT INTO `{{" . $entity->getTableName() . "}}` SET " . $query);
57
58
    // TODO Exceptiion if db_insert_id() is empty
59
    return $entity->dbId = $db->db_insert_id();
60
  }
61
62
}
63