Test Failed
Push — master ( f0470b...53186c )
by Joao
03:23
created

ConnectionManager::beginTransaction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace ByJG\MicroOrm;
4
5
use ByJG\AnyDataset\DbDriverInterface;
6
use ByJG\AnyDataset\Factory;
7
use ByJG\MicroOrm\Exception\OrmBeforeInvalidException;
8
use ByJG\MicroOrm\Exception\OrmInvalidFieldsException;
9
use ByJG\MicroOrm\Exception\TransactionException;
10
use ByJG\Serializer\BinderObject;
11
use ByJG\Util\Uri;
12
13
class ConnectionManager
14
{
15
16
    /**
17
     * @var DbDriverInterface[]
18
     */
19
    protected static $connectionList = [];
20
21
    /**
22
     * @var bool
23
     */
24
    protected static $transaction = false;
25
26
    /**
27
     * @param $uriString
28
     * @return \ByJG\AnyDataset\DbDriverInterface
29
     */
30
    public function addConnection($uriString)
31
    {
32
        if (!isset(self::$connectionList[$uriString])) {
33
            self::$connectionList[$uriString] = Factory::getDbRelationalInstance($uriString);
34
35
            if (self::$transaction) {
36
                self::$connectionList[$uriString]->beginTransaction();
37
            }
38
        }
39
40
        return self::$connectionList[$uriString];
41
    }
42
43
    public function beginTransaction()
44
    {
45
        if (self::$transaction) {
46
            throw new TransactionException("Transaction Already Started");
47
        }
48
49
        self::$transaction = true;
50
        foreach (self::$connectionList as $dbDriver) {
51
            $dbDriver->beginTransaction();
52
        }
53
    }
54
55
56
    public function commitTransaction()
57
    {
58
        if (!self::$transaction) {
59
            throw new TransactionException("There is no Active Transaction");
60
        }
61
62
        self::$transaction = false;
63
        foreach (self::$connectionList as $dbDriver) {
64
            $dbDriver->commitTransaction();
65
        }
66
    }
67
68
69
    public function rollbackTransaction()
70
    {
71
        if (!self::$transaction) {
72
            throw new TransactionException("There is no Active Transaction");
73
        }
74
75
        self::$transaction = false;
76
        foreach (self::$connectionList as $dbDriver) {
77
            $dbDriver->rollbackTransaction();
78
        }
79
    }
80
81
82
}
83