1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Isolate\UnitOfWork; |
4
|
|
|
|
5
|
|
|
use Isolate\UnitOfWork\Entity\ChangeBuilder; |
6
|
|
|
use Isolate\UnitOfWork\Entity\Comparer; |
7
|
|
|
use Isolate\UnitOfWork\Entity\Identifier; |
8
|
|
|
use Isolate\UnitOfWork\Object\Registry; |
9
|
|
|
use Isolate\UnitOfWork\Command\EditCommand; |
10
|
|
|
use Isolate\UnitOfWork\Command\NewCommand; |
11
|
|
|
use Isolate\UnitOfWork\Command\RemoveCommand; |
12
|
|
|
use Isolate\UnitOfWork\Exception\InvalidArgumentException; |
13
|
|
|
use Isolate\UnitOfWork\Exception\RuntimeException; |
14
|
|
|
use Isolate\UnitOfWork\Entity\Definition; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @api |
18
|
|
|
*/ |
19
|
|
|
class UnitOfWork |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Registry |
23
|
|
|
*/ |
24
|
|
|
private $registry; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Identifier |
28
|
|
|
*/ |
29
|
|
|
private $identifier; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ChangeBuilder |
33
|
|
|
*/ |
34
|
|
|
private $changeBuilder; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Comparer |
38
|
|
|
*/ |
39
|
|
|
private $comparer; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var CommandBus |
43
|
|
|
*/ |
44
|
|
|
private $commandBus; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Registry $registry |
48
|
|
|
* @param Identifier $identifier |
49
|
|
|
* @param ChangeBuilder $changeBuilder |
50
|
|
|
* @param Comparer $entityComparer |
51
|
|
|
* @param CommandBus $commandBus |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
Registry $registry, |
55
|
|
|
Identifier $identifier, |
56
|
|
|
ChangeBuilder $changeBuilder, |
57
|
|
|
Comparer $entityComparer, |
58
|
|
|
CommandBus $commandBus |
59
|
|
|
) { |
60
|
|
|
$this->registry = $registry; |
61
|
|
|
$this->identifier = $identifier; |
62
|
|
|
$this->changeBuilder = $changeBuilder; |
63
|
|
|
$this->comparer = $entityComparer; |
64
|
|
|
$this->commandBus = $commandBus; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $entity |
69
|
|
|
* @throws InvalidArgumentException |
70
|
|
|
* @throws RuntimeException |
71
|
|
|
* |
72
|
|
|
* @api |
73
|
|
|
*/ |
74
|
|
|
public function register($entity) |
75
|
|
|
{ |
76
|
|
|
if (!is_object($entity)) { |
77
|
|
|
throw new InvalidArgumentException("Only objects can be registered in Unit of Work."); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!$this->identifier->isEntity($entity)) { |
81
|
|
|
throw new InvalidArgumentException("Only entities can be registered in Unit of Work."); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->registry->register($entity); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $entity |
89
|
|
|
* @return bool |
90
|
|
|
* |
91
|
|
|
* @api |
92
|
|
|
*/ |
93
|
|
|
public function isRegistered($entity) |
94
|
|
|
{ |
95
|
|
|
return $this->registry->isRegistered($entity); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $entity |
100
|
|
|
* @throws InvalidArgumentException |
101
|
|
|
* @throws RuntimeException |
102
|
|
|
* |
103
|
|
|
* @api |
104
|
|
|
*/ |
105
|
|
|
public function remove($entity) |
106
|
|
|
{ |
107
|
|
|
if (!is_object($entity)) { |
108
|
|
|
throw new InvalidArgumentException("Only objects can be registered in Unit of Work."); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (!$this->isRegistered($entity)) { |
112
|
|
|
if (!$this->identifier->isPersisted($entity)) { |
113
|
|
|
throw new RuntimeException("Unit of Work can't remove not persisted entities."); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$this->registry->remove($entity); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @throws InvalidArgumentException |
122
|
|
|
* @throws RuntimeException |
123
|
|
|
* |
124
|
|
|
* @api |
125
|
|
|
*/ |
126
|
|
|
public function commit() |
127
|
|
|
{ |
128
|
|
|
foreach ($this->registry->all() as $entity) { |
129
|
|
|
switch($this->getEntityState($entity)) { |
130
|
|
|
case EntityStates::NEW_ENTITY: |
131
|
|
|
$this->commandBus->dispatch(new NewCommand($entity)); |
132
|
|
|
break; |
133
|
|
|
case EntityStates::EDITED_ENTITY: |
134
|
|
|
$changeSet = $this->changeBuilder->buildChanges($this->registry->getSnapshot($entity), $entity); |
135
|
|
|
$this->commandBus->dispatch(new EditCommand($entity, $changeSet)); |
136
|
|
|
break; |
137
|
|
|
case EntityStates::REMOVED_ENTITY: |
138
|
|
|
$this->commandBus->dispatch(new RemoveCommand($entity)); |
139
|
|
|
break; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$this->registry->cleanRemoved(); |
144
|
|
|
$this->registry->makeNewSnapshots(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @api |
149
|
|
|
*/ |
150
|
|
|
public function rollback() |
151
|
|
|
{ |
152
|
|
|
$this->registry->reset(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param $entity |
157
|
|
|
* @return int |
158
|
|
|
* @throws InvalidArgumentException |
159
|
|
|
* @throws RuntimeException |
160
|
|
|
*/ |
161
|
|
|
private function getEntityState($entity) |
162
|
|
|
{ |
163
|
|
|
if (!is_object($entity)) { |
164
|
|
|
throw new InvalidArgumentException("Only objects can be registered in Unit of Work."); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if (!$this->isRegistered($entity)) { |
168
|
|
|
throw new RuntimeException("Object need to be registered first in the Unit of Work."); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if ($this->registry->isRemoved($entity)) { |
172
|
|
|
return EntityStates::REMOVED_ENTITY; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if (!$this->identifier->isPersisted($entity)) { |
176
|
|
|
return EntityStates::NEW_ENTITY; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if ($this->isChanged($entity)) { |
180
|
|
|
return EntityStates::EDITED_ENTITY; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return EntityStates::PERSISTED_ENTITY; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param $entity |
188
|
|
|
* @return bool |
189
|
|
|
* @throws RuntimeException |
190
|
|
|
*/ |
191
|
|
|
private function isChanged($entity) |
192
|
|
|
{ |
193
|
|
|
return !$this->comparer->areEqual( |
194
|
|
|
$entity, |
195
|
|
|
$this->registry->getSnapshot($entity) |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|