1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Borobudur-Event-Sourcing package. |
4
|
|
|
* |
5
|
|
|
* (c) Hexacodelabs <http://hexacodelabs.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Borobudur\EventSourcing\Storage; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Iqbal Maulana <[email protected]> |
15
|
|
|
* @created 8/22/16 |
16
|
|
|
*/ |
17
|
|
|
trait TransactionalTrait |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
private $transactional = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
private $transactionalLevel = 0; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $watched = array(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return boolean |
36
|
|
|
*/ |
37
|
|
|
public function isTransactional() |
38
|
|
|
{ |
39
|
|
|
return $this->transactional; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return int |
44
|
|
|
*/ |
45
|
|
|
public function getTransactionalLevel() |
46
|
|
|
{ |
47
|
|
|
return $this->transactionalLevel; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Begin transaction |
52
|
|
|
*/ |
53
|
|
|
public function beginTransaction() |
54
|
|
|
{ |
55
|
|
|
if (0 === $this->transactionalLevel) { |
56
|
|
|
$this->transactional = true; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->transactionalLevel += 1; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Rollback transaction |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function rollback() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
if (true === $this->transactional) { |
68
|
|
|
foreach ($this->watched as $table => $records) { |
69
|
|
|
$this->performRollback($table, $records); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->transactionalLevel = 0; |
73
|
|
|
$this->transactional = false; |
74
|
|
|
$this->watched = array(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Commit transaction. |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function commit() |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
if (true === $this->transactional) { |
84
|
|
|
$this->transactionalLevel -= 1; |
85
|
|
|
if (0 === $this->transactionalLevel) { |
86
|
|
|
foreach ($this->watched as $table => $records) { |
87
|
|
|
$this->performCommit($table, $records); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->transactional = false; |
91
|
|
|
$this->watched = array(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $table |
98
|
|
|
* @param array $record |
99
|
|
|
*/ |
100
|
|
|
protected function watch($table, array &$record) |
101
|
|
|
{ |
102
|
|
|
if (true === $this->transactional) { |
103
|
|
|
if (!isset($this->watched[$table])) { |
104
|
|
|
$this->watched[$table] = array(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->watched[$table][] = &$record; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Commit action. |
113
|
|
|
* |
114
|
|
|
* @param string $table |
115
|
|
|
* @param array $records |
116
|
|
|
*/ |
117
|
|
|
abstract protected function performCommit($table, array $records); |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Rollback action. |
121
|
|
|
* |
122
|
|
|
* @param string $table |
123
|
|
|
* @param array $records |
124
|
|
|
*/ |
125
|
|
|
abstract protected function performRollback($table, array $records); |
126
|
|
|
} |
127
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.