ConnectionManager::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

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