Completed
Push — master ( 126036...3fa63d )
by Maik
07:56
created

OrmTransaction   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 74.19%

Importance

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