|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* Copyright (c) 2016 Constantin Galbenu <[email protected]> * |
|
4
|
|
|
******************************************************************************/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Gica\Cqrs\ReadModel; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
use Gica\Cqrs\Event\EventsApplier\EventsApplierOnListener; |
|
10
|
|
|
use Gica\Cqrs\EventStore; |
|
11
|
|
|
use Psr\Log\LoggerInterface; |
|
12
|
|
|
|
|
13
|
|
|
class ReadModelRecreator |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var EventStore |
|
18
|
|
|
*/ |
|
19
|
|
|
private $eventStore; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var EventsApplierOnListener |
|
22
|
|
|
*/ |
|
23
|
|
|
private $eventsApplierOnListener; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var LoggerInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $logger; |
|
28
|
|
|
|
|
29
|
1 |
|
public function __construct( |
|
30
|
|
|
EventStore $eventStore, |
|
31
|
|
|
EventsApplierOnListener $eventsApplierOnListener, |
|
32
|
|
|
LoggerInterface $logger |
|
33
|
|
|
) |
|
34
|
|
|
{ |
|
35
|
1 |
|
$this->eventStore = $eventStore; |
|
36
|
1 |
|
$this->eventsApplierOnListener = $eventsApplierOnListener; |
|
37
|
1 |
|
$this->logger = $logger; |
|
38
|
1 |
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
public function recreateRead(ReadModelInterface $readModel) |
|
41
|
|
|
{ |
|
42
|
1 |
|
$eventClasses = $this->getListenerEventClasses(get_class($readModel)); |
|
43
|
|
|
|
|
44
|
1 |
|
$this->logger->info(print_r($eventClasses, true)); |
|
45
|
1 |
|
$this->logger->info("loading events..."); |
|
46
|
1 |
|
$allEvents = $this->eventStore->loadEventsByClassNames($eventClasses); |
|
47
|
|
|
|
|
48
|
1 |
|
$this->logger->info("applying events..."); |
|
49
|
|
|
|
|
50
|
1 |
|
$this->eventsApplierOnListener->applyEventsOnListener($readModel, $allEvents); |
|
51
|
1 |
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
private function getListenerEventClasses(string $readModelClass) |
|
54
|
|
|
{ |
|
55
|
1 |
|
$result = []; |
|
56
|
|
|
|
|
57
|
1 |
|
$classReflection = new \ReflectionClass($readModelClass); |
|
58
|
|
|
|
|
59
|
1 |
|
foreach ($classReflection->getMethods() as $reflectionMethod) { |
|
60
|
1 |
|
$eventClass = $this->tryToExtractEventClassFromMethod($reflectionMethod); |
|
61
|
|
|
|
|
62
|
1 |
|
if (false !== $eventClass) { |
|
63
|
1 |
|
$result[] = $eventClass; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
return $result; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
private function tryToExtractEventClassFromMethod(\ReflectionMethod $reflectionMethod) |
|
71
|
|
|
{ |
|
72
|
1 |
|
foreach ($reflectionMethod->getParameters() as $reflectionParameter) { |
|
73
|
1 |
|
$eventClass = $this->tryToExtractEventClassFromParameter($reflectionParameter); |
|
74
|
|
|
|
|
75
|
1 |
|
if (false !== $eventClass) { |
|
76
|
1 |
|
return $eventClass; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
private function tryToExtractEventClassFromParameter(\ReflectionParameter $reflectionParameter) |
|
84
|
|
|
{ |
|
85
|
1 |
|
if ($reflectionParameter->getClass() && is_subclass_of($reflectionParameter->getClass()->getName(), \Gica\Cqrs\Event::class)) { |
|
|
|
|
|
|
86
|
1 |
|
return $reflectionParameter->getClass()->getName(); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
} |