Total Complexity | 6 |
Total Lines | 54 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
17 | class Transaction |
||
18 | { |
||
19 | |||
20 | use FactoryTrait; |
||
21 | |||
22 | /** |
||
23 | * @var bool |
||
24 | */ |
||
25 | protected $committed = false; |
||
26 | |||
27 | /** |
||
28 | * @var DB |
||
29 | */ |
||
30 | protected $db; |
||
31 | |||
32 | /** |
||
33 | * Begins the transaction/savepoint. |
||
34 | * |
||
35 | * @param DB $db |
||
36 | */ |
||
37 | public function __construct(DB $db) |
||
38 | { |
||
39 | $this->db = $db; |
||
40 | $db->beginTransaction(); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Rolls back if the instance wasn't committed. |
||
45 | */ |
||
46 | public function __destruct() |
||
47 | { |
||
48 | if (!$this->committed) { |
||
49 | $this->db->rollBack(); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Commits the transaction/savepoint. |
||
55 | * |
||
56 | * This is safe to call multiple times, it won't have any effect after the first time. |
||
57 | * |
||
58 | * @return true |
||
59 | */ |
||
60 | public function commit(): bool |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @return bool |
||
67 | */ |
||
68 | final public function wasCommitted(): bool |
||
71 | } |
||
72 | } |
||
73 |