|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (c) 2013 Janos Szurovecz |
|
4
|
|
|
* |
|
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
6
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
7
|
|
|
* the Software without restriction, including without limitation the rights to |
|
8
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
|
9
|
|
|
* of the Software, and to permit persons to whom the Software is furnished to do |
|
10
|
|
|
* so, subject to the following conditions: |
|
11
|
|
|
* |
|
12
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
13
|
|
|
* copies or substantial portions of the Software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
21
|
|
|
* SOFTWARE. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace predaddy\domain; |
|
25
|
|
|
|
|
26
|
|
|
use precore\lang\IllegalStateException; |
|
27
|
|
|
use precore\lang\Object; |
|
28
|
|
|
use precore\lang\ObjectInterface; |
|
29
|
|
|
use precore\util\Objects; |
|
30
|
|
|
use precore\util\Preconditions; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* AggregateRoot implementation which provides features for handling |
|
34
|
|
|
* state hash and sending DomainEvents. |
|
35
|
|
|
* |
|
36
|
|
|
* All Events which extends AbstractDomainEvent will be filled |
|
37
|
|
|
* with the proper AggregateId and state hash when they are being raised. |
|
38
|
|
|
* |
|
39
|
|
|
* If you want to use the state hash feature, you can follow two ways: |
|
40
|
|
|
* - you persist the stateHash member variable |
|
41
|
|
|
* - you define your own state hash field in your class. In this case you may need to override the following methods: |
|
42
|
|
|
* - calculateNextStateHash() |
|
43
|
|
|
* - setStateHash() |
|
44
|
|
|
* - stateHash() |
|
45
|
|
|
* |
|
46
|
|
|
* @author Janos Szurovecz <[email protected]> |
|
47
|
|
|
*/ |
|
48
|
|
|
abstract class AbstractAggregateRoot extends Object implements AggregateRoot |
|
49
|
|
|
{ |
|
50
|
|
|
/** |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
private $stateHash; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $expectedHash |
|
57
|
|
|
* @throws IllegalStateException |
|
58
|
|
|
*/ |
|
59
|
|
|
final public function failWhenStateHashViolation($expectedHash) |
|
60
|
|
|
{ |
|
61
|
|
|
Preconditions::checkState( |
|
62
|
|
|
$this->stateHash() === $expectedHash, |
|
63
|
|
|
'Concurrency Violation: Stale data detected. Entity was already modified.' |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* The basic behavior that the state hash of the AggregateRoot |
|
69
|
|
|
* is the ID of the last event. |
|
70
|
|
|
* |
|
71
|
|
|
* @param DomainEvent $raisedEvent |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function calculateNextStateHash(DomainEvent $raisedEvent) |
|
75
|
|
|
{ |
|
76
|
|
|
return $raisedEvent->identifier(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function setStateHash($stateHash) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->stateHash = $stateHash; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Updates the state hash and sends the DomainEvent to the EventPublisher. |
|
86
|
|
|
* It also automatically fills the raised event if it extends AbstractDomainEvent. |
|
87
|
|
|
* |
|
88
|
|
|
* @param DomainEvent $event |
|
89
|
|
|
*/ |
|
90
|
|
|
final protected function raise(DomainEvent $event) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->setStateHash($this->calculateNextStateHash($event)); |
|
93
|
|
|
if ($event instanceof AbstractDomainEvent) { |
|
94
|
|
|
AbstractDomainEvent::initEvent($event, $this->getId(), $this->stateHash()); |
|
95
|
|
|
} |
|
96
|
|
|
EventPublisher::instance()->post($event); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return null|string |
|
101
|
|
|
*/ |
|
102
|
|
|
public function stateHash() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->stateHash; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function toString() |
|
108
|
|
|
{ |
|
109
|
|
|
return Objects::toStringHelper($this) |
|
110
|
|
|
->add('id', $this->getId()) |
|
111
|
|
|
->add('stateHash', $this->stateHash()) |
|
112
|
|
|
->toString(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function equals(ObjectInterface $object = null) |
|
116
|
|
|
{ |
|
117
|
|
|
// instanceof has to be used here, since aggregates might be proxied (e.g. Doctrine) |
|
118
|
|
|
return $object instanceof AbstractAggregateRoot |
|
119
|
|
|
&& Objects::equal($this->getId(), $object->getId()); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|