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

DbTransaction   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 10
Bugs 0 Features 0
Metric Value
dl 0
loc 118
rs 10
c 10
b 0
f 0
ccs 0
cts 45
cp 0
wmc 15
lcom 1
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B check() 0 19 6
A start() 0 20 3
A commit() 0 7 1
A rollback() 0 7 1
A db_transaction_clear() 0 8 1
A getNextQueryTransactionId() 0 3 2
1
<?php
2
3
/**
4
 * Created by Gorlum 31.07.2016 12:08
5
 */
6
7
namespace DBAL;
8
9
use \classSupernova;
10
use Common\GlobalContainer;
11
use \db_mysql;
12
13
class DbTransaction {
14
15
  protected $db_in_transaction = false;
16
  protected $transaction_id = 0;
17
18
  /**
19
   * @var db_mysql $db
20
   */
21
  protected $db;
22
23
  /**
24
   * @var \SnCache $snCache
25
   */
26
  protected $snCache;
27
28
  /**
29
   * DbTransaction constructor.
30
   *
31
   * @param GlobalContainer $gc
32
   * @param db_mysql $db
33
   */
34
  public function __construct($gc, $db) {
0 ignored issues
show
Unused Code introduced by
The parameter $gc is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
    $this->db = $db;
36
    $this->snCache = $db->snCache;
37
  }
38
39
  /**
40
   * Эта функция проверяет статус транзакции
41
   *
42
   * Это - низкоуровневая функция. В нормальном состоянии движка её сообщения никогда не будут видны
43
   *
44
   * @param null|bool $status Должна ли быть запущена транзакция в момент проверки
45
   *   <p>null - транзакция НЕ должна быть запущена</p>
46
   *   <p>true - транзакция должна быть запущена - для совместимости с $for_update</p>
47
   *   <p>false - всё равно - для совместимости с $for_update</p>
48
   *
49
   * @return bool Текущий статус транзакции
50
   */
51
  public function check($status = null) {
52
    $error_msg = false;
53
    if ($status && !$this->db_in_transaction) {
54
      $error_msg = 'No transaction started for current operation';
55
    } elseif ($status === null && $this->db_in_transaction) {
56
      $error_msg = 'Transaction is already started';
57
    }
58
59
    if (!empty($error_msg)) {
60
      // TODO - Убрать позже
61
      print('<h1>СООБЩИТЕ ЭТО АДМИНУ: sn_db_transaction_check() - ' . $error_msg . '</h1>');
62
      $backtrace = debug_backtrace();
63
      array_shift($backtrace);
64
      pdump($backtrace);
65
      die($error_msg);
66
    }
67
68
    return $this->db_in_transaction;
69
  }
70
71
  public function start($level = '') {
72
    $this->check(null);
73
74
    $level ? $this->db->doSql('SET TRANSACTION ISOLATION LEVEL ' . $level) : false;
75
76
    $this->transaction_id++;
77
    $this->db->doSql('START TRANSACTION');
78
//pdump(debug_backtrace());pdie();
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% 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...
79
80
    if (classSupernova::$gc->config->db_manual_lock_enabled) {
81
      classSupernova::$gc->config->db_loadItem('var_db_manually_locked');
0 ignored issues
show
Bug introduced by
The method db_loadItem does only exist in classConfig, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
82
      classSupernova::$gc->config->db_saveItem('var_db_manually_locked', SN_TIME_SQL);
0 ignored issues
show
Bug introduced by
The method db_saveItem does only exist in classConfig, but not in Closure.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
83
    }
84
85
    $this->db_in_transaction = true;
86
    $this->db->snCache->locatorReset();
87
    $this->db->snCache->queriesReset();
88
89
    return $this->transaction_id;
90
  }
91
92
  // TODO - move changeset data and methods somewhere
93
  public function commit() {
94
    $this->check(true);
95
96
    $this->db->doSql('COMMIT');
97
98
    return $this->db_transaction_clear();
99
  }
100
101
  public function rollback() {
102
    // TODO - вообще-то тут тоже надо проверять есть ли транзакция
103
104
    $this->db->doSql('ROLLBACK');
105
106
    return $this->db_transaction_clear();
107
  }
108
109
  protected function db_transaction_clear() {
110
    $this->db->snCache->cache_lock_unset_all();
111
112
    $this->db_in_transaction = false;
113
    $this->transaction_id++;
114
115
    return $this->transaction_id;
116
  }
117
118
  /**
119
   * Get Transaction ID for next query
120
   *
121
   * If transaction is in progress - next ID would be equal to current
122
   * Otherwise we should increase ID and return this value
123
   *
124
   * @return int
125
   */
126
  public function getNextQueryTransactionId() {
127
    return $this->check(false) ? $this->transaction_id : $this->transaction_id++;
128
  }
129
130
}
131