|
1
|
|
|
<?php namespace C4tech\RayEmitter\Domain; |
|
2
|
|
|
|
|
3
|
|
|
use C4tech\RayEmitter\Contracts\Domain\Command as CommandInterface; |
|
4
|
|
|
use C4tech\RayEmitter\Contracts\Domain\Repository as RepositoryInterface; |
|
5
|
|
|
use C4tech\RayEmitter\Facades\EventStore; |
|
6
|
|
|
use C4tech\RayEmitter\Exceptions\OutdatedSequence; |
|
7
|
|
|
use C4tech\RayEmitter\Exceptions\SequenceMismatch; |
|
8
|
|
|
|
|
9
|
|
|
abstract class Repository implements RepositoryInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @inheritDoc |
|
13
|
|
|
*/ |
|
14
|
1 |
|
public static function get($identifier) |
|
15
|
|
|
{ |
|
16
|
1 |
|
$aggregate = static::find($identifier); |
|
17
|
|
|
|
|
18
|
1 |
|
return $aggregate->getEntity(); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @inheritDoc |
|
23
|
|
|
*/ |
|
24
|
4 |
|
public static function handle(CommandInterface $command) |
|
25
|
|
|
{ |
|
26
|
4 |
|
$aggregate = static::find($command->getAggregateId()); |
|
27
|
|
|
|
|
28
|
|
|
// Optimistic concurrency handling |
|
29
|
4 |
|
$expected = $command->getExpectedSequence(); |
|
30
|
4 |
|
$current = $aggregate->getSequence(); |
|
31
|
4 |
|
if ($expected < $current) { |
|
32
|
1 |
|
throw new OutdatedSequence( |
|
33
|
1 |
|
sprintf( |
|
34
|
1 |
|
'The Aggregate %s has newer data than expected (%s v %s).', |
|
35
|
1 |
|
get_class($aggregate), |
|
36
|
1 |
|
$expected, |
|
37
|
|
|
$current |
|
38
|
1 |
|
), |
|
39
|
|
|
409 |
|
40
|
1 |
|
); |
|
41
|
3 |
|
} elseif ($expected > $current) { |
|
42
|
1 |
|
throw new SequenceMismatch( |
|
43
|
1 |
|
sprintf( |
|
44
|
1 |
|
'The Aggregate %s is expected to have more data than it does (%s v %s).', |
|
45
|
1 |
|
get_class($aggregate), |
|
46
|
1 |
|
$expected, |
|
47
|
|
|
$current |
|
48
|
1 |
|
), |
|
49
|
|
|
422 |
|
50
|
1 |
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
$identifier = null; |
|
54
|
|
|
|
|
55
|
|
|
// Queue event for storage |
|
56
|
2 |
|
if ($event = $aggregate->handle($command)) { |
|
57
|
1 |
|
$aggregate->apply($event); |
|
58
|
1 |
|
EventStore::saveEvent($event); |
|
59
|
1 |
|
$identifier = $event->getId(); |
|
60
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
return $identifier; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Create |
|
67
|
|
|
* |
|
68
|
|
|
* Generate a new Aggregate with no history. |
|
69
|
|
|
* @return Aggregate |
|
70
|
|
|
*/ |
|
71
|
|
|
protected static function create() |
|
72
|
|
|
{ |
|
73
|
|
|
throw new Exception('The ' . static::class . '::create method must be defined'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Find |
|
78
|
|
|
* |
|
79
|
|
|
* Restore an existing Aggregate from the recorded events related to it. |
|
80
|
|
|
* @param void|string $identifier Aggregate root entity identifier. |
|
81
|
|
|
* @return Aggregate |
|
82
|
|
|
*/ |
|
83
|
6 |
|
protected static function &find($identifier = null) |
|
84
|
|
|
{ |
|
85
|
6 |
|
$aggregate = static::create(); |
|
86
|
|
|
|
|
87
|
6 |
|
if ($identifier) { |
|
|
|
|
|
|
88
|
6 |
|
$events = EventStore::getFor($identifier); |
|
89
|
6 |
|
$aggregate->hydrate($events); |
|
90
|
6 |
|
} |
|
91
|
|
|
|
|
92
|
6 |
|
return $aggregate; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: