Passed
Push — int-types ( fff5d9...402d11 )
by Sam
05:52
created

MySQLTransactionManager::transactionRollback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ORM\Connect;
4
5
use SilverStripe\Dev\Deprecation;
6
7
/**
8
 * TransactionManager that executes MySQL-compatible transaction control queries
9
 */
10
class MySQLTransactionManager implements TransactionManager
11
{
12
    protected $dbConn;
13
14
    protected $inTransaction = false;
15
16
    public function __construct(Database $dbConn)
17
    {
18
        $this->$dbConn = $dbConn;
19
    }
20
21
    /**
22
     * @inherit
23
     */
24
    public function transactionStart($transactionMode = false, $sessionCharacteristics = false)
25
    {
26
        if ($transactionMode || $sessionCharacteristics) {
27
            Deprecation::notice(
28
                '4.3',
29
                '$transactionMode and $sessionCharacteristics are deprecated and will be removed in SS5'
30
            );
31
        }
32
33
        if ($this->inTransaction) {
34
            throw new DatabaseException(
35
                "Already in transaction, can't start another. Consider decorating with NestedTransactionManager."
36
            );
37
        }
38
39
        // This sets the isolation level for the NEXT transaction, not the current one.
40
        if ($transactionMode) {
41
            $this->dbConn->query('SET TRANSACTION ' . $transactionMode);
42
        }
43
44
        $this->dbConn->query('START TRANSACTION');
45
46
        if ($sessionCharacteristics) {
47
            $this->dbConn->query('SET SESSION TRANSACTION ' . $sessionCharacteristics);
48
        }
49
50
        $this->inTransaction = true;
51
        return true;
52
    }
53
54
    /**
55
     * @inherit
56
     */
57
    public function transactionEnd($chain = false)
58
    {
59
        if (!$this->inTransaction) {
60
            throw new DatabaseException("Not in transaction, can't end.");
61
        }
62
63
        if ($chain) {
64
            user_error(
65
                "transactionEnd() chain argument no longer implemented. Use NestedTransactionManager",
66
                E_USER_WARNING
67
            );
68
        }
69
70
        $this->query('COMMIT');
0 ignored issues
show
Bug introduced by
The method query() does not exist on SilverStripe\ORM\Connect\MySQLTransactionManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        $this->/** @scrutinizer ignore-call */ 
71
               query('COMMIT');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
72
        $this->inTransaction = false;
73
        return true;
74
    }
75
76
    /**
77
     * @inherit
78
     */
79
    public function transactionRollback()
80
    {
81
        if (!$this->inTransaction) {
82
            throw new DatabaseException("Not in transaction, can't roll back.");
83
        }
84
85
        $this->query('ROLLBACK');
86
87
        $this->inTransaction = false;
88
        return true;
89
    }
90
91
    /**
92
     * @inherit
93
     */
94
    public function transactionDepth()
95
    {
96
        return (int)$this->inTransaction;
97
    }
98
}
99