Test Failed
Push — master ( 53186c...5038cf )
by Joao
03:09
created

ConnectionManager::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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
     * It has an active transaction?
23
     *
24
     * @var bool
25
     */
26
    protected static $transaction = false;
27
28
    /**
29
     * Add or reuse a connection
30
     *
31
     * @param $uriString
32
     * @return \ByJG\AnyDataset\DbDriverInterface
33
     */
34
    public function addConnection($uriString)
35
    {
36
        if (!isset(self::$connectionList[$uriString])) {
37
            self::$connectionList[$uriString] = Factory::getDbRelationalInstance($uriString);
38
39
            if (self::$transaction) {
40
                self::$connectionList[$uriString]->beginTransaction();
41
            }
42
        }
43
44
        return self::$connectionList[$uriString];
45
    }
46
47
    /**
48
     * @return int
49
     */
50
    public function count()
51
    {
52
        return count(self::$connectionList);
53
    }
54
55
    /**
56
     * Start a database transaction with the opened connections
57
     *
58
     * @throws \ByJG\MicroOrm\Exception\TransactionException
59
     */
60
    public function beginTransaction()
61
    {
62
        if (self::$transaction) {
63
            throw new TransactionException("Transaction Already Started");
64
        }
65
66
        self::$transaction = true;
67
        foreach (self::$connectionList as $dbDriver) {
68
            $dbDriver->beginTransaction();
69
        }
70
    }
71
72
73
    /**
74
     * Commit all open transactions
75
     *
76
     * @throws \ByJG\MicroOrm\Exception\TransactionException
77
     */
78
    public function commitTransaction()
79
    {
80
        if (!self::$transaction) {
81
            throw new TransactionException("There is no Active Transaction");
82
        }
83
84
        self::$transaction = false;
85
        foreach (self::$connectionList as $dbDriver) {
86
            $dbDriver->commitTransaction();
87
        }
88
    }
89
90
91
    /**
92
     * Rollback all open transactions
93
     *
94
     * @throws \ByJG\MicroOrm\Exception\TransactionException
95
     */
96
    public function rollbackTransaction()
97
    {
98
        if (!self::$transaction) {
99
            throw new TransactionException("There is no Active Transaction");
100
        }
101
102
        self::$transaction = false;
103
        foreach (self::$connectionList as $dbDriver) {
104
            $dbDriver->rollbackTransaction();
105
        }
106
    }
107
108
    /**
109
     * Destroy all connections
110
     */
111
    public function destroy()
112
    {
113
        foreach (self::$connectionList as $dbDriver) {
114
            if (self::$transaction) {
115
                $dbDriver->commitTransaction();
116
            }
117
        }
118
        self::$transaction = false;
119
        self::$connectionList = [];
120
    }
121
}
122