|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Borobudur-Cqrs package. |
|
4
|
|
|
* (c) 2016 Hexacodelabs <http://hexacodelabs.com> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Borobudur\Cqrs; |
|
11
|
|
|
|
|
12
|
|
|
use Borobudur\Cqrs\ReadModel\Storage\TransactionalInterface; |
|
13
|
|
|
use Closure; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author Iqbal Maulana <[email protected]> |
|
17
|
|
|
* @created 8/2/16 |
|
18
|
|
|
*/ |
|
19
|
|
|
class UnitOfWorkTransaction implements TransactionalInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var TransactionalInterface[] |
|
23
|
|
|
*/ |
|
24
|
|
|
private $handlers = array(); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param TransactionalInterface[] $handlers |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(array $handlers = array()) |
|
32
|
|
|
{ |
|
33
|
|
|
foreach ($handlers as $handler) { |
|
34
|
|
|
$this->add($handler); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Add transactional handler. |
|
40
|
|
|
* |
|
41
|
|
|
* @param TransactionalInterface $handler |
|
42
|
|
|
*/ |
|
43
|
|
|
public function add(TransactionalInterface $handler) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->handlers[] = $handler; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Begin transaction |
|
50
|
|
|
*/ |
|
51
|
|
|
public function beginTransaction() |
|
52
|
|
|
{ |
|
53
|
|
|
$this->execute( |
|
54
|
|
|
function (TransactionalInterface $handler) { |
|
55
|
|
|
$handler->beginTransaction(); |
|
56
|
|
|
} |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Rollback transaction |
|
62
|
|
|
*/ |
|
63
|
|
|
public function rollback() |
|
64
|
|
|
{ |
|
65
|
|
|
$this->execute( |
|
66
|
|
|
function (TransactionalInterface $handler) { |
|
67
|
|
|
$handler->rollback(); |
|
68
|
|
|
} |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Commit transaction. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function commit() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->execute( |
|
78
|
|
|
function (TransactionalInterface $handler) { |
|
79
|
|
|
$handler->commit(); |
|
80
|
|
|
} |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param Closure $closure |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function execute(Closure $closure) |
|
88
|
|
|
{ |
|
89
|
|
|
foreach ($this->handlers as $handler) { |
|
90
|
|
|
$closure($handler); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
|
|
public function isTransactional() |
|
98
|
|
|
{ |
|
99
|
|
|
return count($this->handlers) && $this->handlers[0]->isTransactional(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return int |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getTransactionalLevel() |
|
106
|
|
|
{ |
|
107
|
|
|
return count($this->handlers) ? $this->handlers[0]->getTransactionalLevel() : 0; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|