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
|
|
|
* Aggregate Cache |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected static $aggregates = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @inheritDoc |
19
|
|
|
*/ |
20
|
1 |
|
public static function get($identifier) |
21
|
|
|
{ |
22
|
1 |
|
$aggregate = static::find($identifier); |
23
|
|
|
|
24
|
1 |
|
return $aggregate->getEntity(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritDoc |
29
|
|
|
*/ |
30
|
4 |
|
public static function handle(CommandInterface $command) |
31
|
|
|
{ |
32
|
4 |
|
$aggregate = static::find($command->getAggregateId()); |
33
|
|
|
|
34
|
|
|
// Optimistic concurrency handling |
35
|
4 |
|
$expected = $command->getExpectedSequence(); |
36
|
4 |
|
$current = $aggregate->getSequence(); |
37
|
4 |
|
if ($expected < $current) { |
38
|
1 |
|
throw new OutdatedSequence( |
39
|
1 |
|
sprintf( |
40
|
1 |
|
'The Aggregate %s has newer data than expected.', |
41
|
1 |
|
get_class($aggregate) |
42
|
1 |
|
), |
43
|
|
|
409 |
44
|
1 |
|
); |
45
|
3 |
|
} elseif ($expected > $current) { |
46
|
1 |
|
throw new SequenceMismatch( |
47
|
1 |
|
sprintf( |
48
|
1 |
|
'The Aggregate %s is expected to have more data than it does', |
49
|
1 |
|
get_class($aggregate) |
50
|
1 |
|
), |
51
|
|
|
422 |
52
|
1 |
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
$identifier = null; |
56
|
|
|
|
57
|
|
|
// Queue event for storage |
58
|
2 |
|
if ($event = $aggregate->handle($command)) { |
59
|
1 |
|
$aggregate->apply($event); |
60
|
1 |
|
EventStore::enqueue($event); |
61
|
1 |
|
$identifier = $event->getId(); |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
// Cache the aggregate in memory if it is not already. |
66
|
2 |
|
if (!empty($identifier) && !isset(self::$aggregates[$identifier])) { |
67
|
1 |
|
self::$aggregates[$identifier] = $aggregate; |
68
|
1 |
|
} |
69
|
|
|
|
70
|
2 |
|
return $identifier; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Create |
75
|
|
|
* |
76
|
|
|
* Generate a new Aggregate with no history. |
77
|
|
|
* @return Aggregate |
78
|
|
|
*/ |
79
|
|
|
protected static function create() |
80
|
|
|
{ |
81
|
|
|
throw new Exception('The ' . static::class . '::create method must be defined'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Find |
86
|
|
|
* |
87
|
|
|
* Restore an existing Aggregate from the recorded events related to it. |
88
|
|
|
* @param void|string $identifier Aggregate root entity identifier. |
89
|
|
|
* @return Aggregate |
90
|
|
|
*/ |
91
|
6 |
|
protected static function &find($identifier = null) |
92
|
|
|
{ |
93
|
6 |
|
$aggregate = static::create(); |
94
|
|
|
|
95
|
6 |
|
if ($identifier) { |
|
|
|
|
96
|
|
|
// Cache hydrated aggregate |
97
|
6 |
|
if (!isset(self::$aggregates[$identifier])) { |
98
|
6 |
|
$events = EventStore::getFor($identifier); |
99
|
6 |
|
$aggregate->hydrate($events); |
100
|
|
|
|
101
|
6 |
|
self::$aggregates[$identifier] = $aggregate; |
102
|
6 |
|
} |
103
|
|
|
|
104
|
6 |
|
$aggregate = self::$aggregates[$identifier]; |
105
|
6 |
|
} |
106
|
|
|
|
107
|
6 |
|
return $aggregate; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: