OrmTransaction   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80.77%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 83
ccs 21
cts 26
cp 0.8077
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A startTX() 0 12 2
A commitTX() 0 14 3
B rollBackTX() 0 24 5
1
<?php
2
namespace Nkey\Caribu\Orm;
3
4
use PDOException;
5
6
/**
7
 * Transaction related functionality
8
 *
9
 * This class is part of Caribu package
10
 *
11
 * @author Maik Greubel <[email protected]>
12
 */
13
trait OrmTransaction
14
{
15
    /**
16
     * Include the connection related functionality
17
     */
18
    use OrmConnection;
19
20
    /**
21
     * The stack of open transactions
22
     *
23
     * @var int
24
     */
25
    private $transactionStack = 0;
26
27
    /**
28
     * Begin a new transaction
29
     *
30
     * @return \PDO
31
     */
32 29
    public function startTX()
33
    {
34 29
        $connection = $this->getConnection();
35
36 29
        if (!$connection->inTransaction()) {
37 29
            $connection->beginTransaction();
38
        }
39
40 29
        $this->transactionStack++;
41
42 29
        return $connection;
43
    }
44
45
    /**
46
     * Try to commit the complete transaction stack
47
     *
48
     * @throws OrmException
49
     * @throws PDOException
50
     */
51 27
    public function commitTX()
52
    {
53 27
        $connection = $this->getConnection();
54
55 27
        if (!$connection->inTransaction()) {
56
            throw new OrmException("Transaction is not open");
57
        }
58
59 27
        $this->transactionStack--;
60
61 27
        if ($this->transactionStack === 0) {
62 27
            $connection->commit();
63
        }
64 27
    }
65
66
    /**
67
     * Rollback the complete stack
68
     *
69
     * @return OrmException either previous exception or new occured during rollback containing previous
70
     */
71 2
    public function rollBackTX(\Exception $previousException = null)
72
    {
73 2
        $connection = $this->getConnection();
74
75 2
        $this->transactionStack = 0; // Yes, we just ignore any error and reset the transaction stack here
76
77 2
        if (!$connection) {
78
            $previousException = new OrmException("Connection not open", array(), 101, $previousException);
79
        }
80
81 2
        if (!$connection->inTransaction()) {
82
            $previousException = new OrmException("Transaction not open", array(), 102, $previousException);
83
        }
84
85
        try {
86 2
            if (!$connection->rollBack()) {
87 2
                $previousException = new OrmException("Could not rollback!", array(), 103, $previousException);
88
            }
89
        } catch (\PDOException $exception) {
90
            $previousException = OrmException::fromPrevious($exception);
91
        }
92
93 2
        return $previousException;
94
    }
95
}
96