1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/* |
3
|
|
|
* Copyright (c) 2012-2014 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\eventsourcing; |
25
|
|
|
|
26
|
|
|
use Iterator; |
27
|
|
|
use predaddy\domain\AbstractAggregateRoot; |
28
|
|
|
use predaddy\domain\DomainEvent; |
29
|
|
|
use predaddy\eventhandling\EventBus; |
30
|
|
|
use predaddy\eventhandling\EventFunctionDescriptorFactory; |
31
|
|
|
use predaddy\messagehandling\MessageBus; |
32
|
|
|
use predaddy\messagehandling\annotation\Subscribe; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* You can send an event which will be handled by all handler methods |
36
|
|
|
* inside the aggregate root, after that they will be sent to all outer event handlers. |
37
|
|
|
* |
38
|
|
|
* Handler methods must be annotated with "Subscribe" |
39
|
|
|
* and must be private or protected methods. You can override this behaviour |
40
|
|
|
* with setInnerMessageBusFactory() method. |
41
|
|
|
* |
42
|
|
|
* @author Janos Szurovecz <[email protected]> |
43
|
|
|
*/ |
44
|
|
|
abstract class AbstractEventSourcedAggregateRoot extends AbstractAggregateRoot implements EventSourcedAggregateRoot |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* @var EventSourcingEventHandlerDescriptorFactory |
48
|
|
|
*/ |
49
|
|
|
private static $descriptorFactory; |
50
|
|
|
|
51
|
|
|
public static function init() |
52
|
|
|
{ |
53
|
|
|
self::$descriptorFactory = new EventSourcingEventHandlerDescriptorFactory( |
54
|
|
|
new EventFunctionDescriptorFactory() |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param AbstractEventSourcedAggregateRoot $aggregateRoot |
60
|
|
|
* @return EventBus |
61
|
|
|
*/ |
62
|
|
|
private static function createInnerEventBus(AbstractEventSourcedAggregateRoot $aggregateRoot) |
63
|
|
|
{ |
64
|
|
|
$bus = EventBus::builder() |
65
|
|
|
->withIdentifier(static::className()) |
66
|
|
|
->withHandlerDescriptorFactory(self::$descriptorFactory) |
67
|
|
|
->build(); |
68
|
|
|
$bus->register($aggregateRoot); |
69
|
|
|
return $bus; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Useful in case of Event Sourcing. |
74
|
|
|
* |
75
|
|
|
* @see EventSourcingRepository |
76
|
|
|
* @param Iterator $events DomainEvent iterator |
77
|
|
|
*/ |
78
|
|
|
final public function loadFromHistory(Iterator $events) |
79
|
|
|
{ |
80
|
|
|
$bus = self::createInnerEventBus($this); |
81
|
|
|
foreach ($events as $event) { |
82
|
|
|
$this->handleEventInAggregate($event, $bus); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Fire a domain event from a handler method. |
88
|
|
|
* |
89
|
|
|
* @param DomainEvent $event |
90
|
|
|
*/ |
91
|
|
|
final protected function apply(DomainEvent $event) |
92
|
|
|
{ |
93
|
|
|
$this->handleEventInAggregate($event); |
94
|
|
|
parent::raise($event); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Updates stateHash field when replaying events. Should not be called directly. |
99
|
|
|
* |
100
|
|
|
* @Subscribe |
101
|
|
|
* @param DomainEvent $event |
102
|
|
|
*/ |
103
|
|
|
final protected function updateStateHash(DomainEvent $event) |
104
|
|
|
{ |
105
|
|
|
$this->setStateHash($event->stateHash()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function handleEventInAggregate(DomainEvent $event, MessageBus $innerBus = null) |
109
|
|
|
{ |
110
|
|
|
if ($innerBus === null) { |
111
|
|
|
$innerBus = self::createInnerEventBus($this); |
112
|
|
|
} |
113
|
|
|
$innerBus->post($event); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
AbstractEventSourcedAggregateRoot::init(); |
117
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.