|
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 InvalidArgumentException; |
|
27
|
|
|
use precore\lang\Object; |
|
28
|
|
|
use precore\lang\ObjectClass; |
|
29
|
|
|
use precore\util\Preconditions; |
|
30
|
|
|
use predaddy\domain\AggregateId; |
|
31
|
|
|
use predaddy\domain\AggregateRoot; |
|
32
|
|
|
use predaddy\domain\EventStore; |
|
33
|
|
|
use predaddy\domain\Repository; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Should be used for event sourcing. |
|
37
|
|
|
* |
|
38
|
|
|
* @author Janos Szurovecz <[email protected]> |
|
39
|
|
|
*/ |
|
40
|
|
|
class EventSourcingRepository extends Object implements Repository |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* @var EventStore |
|
44
|
|
|
*/ |
|
45
|
|
|
private $eventStore; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param EventStore $eventStore |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(EventStore $eventStore) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->eventStore = $eventStore; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return EventStore |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getEventStore() |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->eventStore; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Initializes the stored aggregate with its events persisted in event store. |
|
65
|
|
|
* |
|
66
|
|
|
* @param AggregateId $aggregateId |
|
67
|
|
|
* @return EventSourcedAggregateRoot |
|
68
|
|
|
* @throws InvalidArgumentException If the $aggregateId is invalid |
|
69
|
|
|
*/ |
|
70
|
|
|
public function load(AggregateId $aggregateId) |
|
71
|
|
|
{ |
|
72
|
|
|
$aggregateRootClass = ObjectClass::forName($aggregateId->aggregateClass()); |
|
73
|
|
|
$aggregate = null; |
|
74
|
|
|
$stateHash = null; |
|
75
|
|
|
if ($this->eventStore instanceof SnapshotEventStore) { |
|
76
|
|
|
$aggregate = $this->eventStore->loadSnapshot($aggregateId); |
|
77
|
|
|
$stateHash = $aggregate === null |
|
78
|
|
|
? null |
|
79
|
|
|
: $aggregate->stateHash(); |
|
80
|
|
|
} |
|
81
|
|
|
$events = $this->eventStore->getEventsFor($aggregateId, $stateHash); |
|
82
|
|
|
if ($aggregate === null) { |
|
83
|
|
|
Preconditions::checkArgument($events->valid(), 'Aggregate with ID [%s] does not exist', $aggregateId); |
|
|
|
|
|
|
84
|
|
|
$aggregate = $aggregateRootClass->newInstanceWithoutConstructor(); |
|
85
|
|
|
} |
|
86
|
|
|
$aggregateRootClass->cast($aggregate); |
|
87
|
|
|
$aggregate->loadFromHistory($events); |
|
88
|
|
|
return $aggregate; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Persisting the given $aggregateRoot. |
|
93
|
|
|
* |
|
94
|
|
|
* @param AggregateRoot $aggregateRoot |
|
95
|
|
|
*/ |
|
96
|
|
|
public function save(AggregateRoot $aggregateRoot) |
|
97
|
|
|
{ |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: