1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/cqrs-tools |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\CQRSTools\Application\Projection; |
11
|
|
|
|
12
|
|
|
use League\Event\EventInterface; |
13
|
|
|
use Slick\CQRSTools\Application\Projection\ProjectionEventListener\ListenerEventStore; |
14
|
|
|
use Slick\CQRSTools\Domain\Event\StoredEvent; |
15
|
|
|
use Slick\CQRSTools\Domain\Projection\ProjectorStateLedger; |
16
|
|
|
use Slick\CQRSTools\Event; |
17
|
|
|
use Slick\CQRSTools\Event\AbstractListener; |
18
|
|
|
use Slick\CQRSTools\Event\EventListener; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* ProjectionEventListener |
22
|
|
|
* |
23
|
|
|
* @package Slick\CQRSTools\Application\Projection |
24
|
|
|
*/ |
25
|
|
|
final class ProjectionEventListener extends AbstractListener implements EventListener |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var ListenerEventStore |
29
|
|
|
*/ |
30
|
|
|
private $eventStore; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $projectors; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var EventHandlingStrategy |
39
|
|
|
*/ |
40
|
|
|
private $strategy; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ProjectorStateLedger |
44
|
|
|
*/ |
45
|
|
|
private $ledger; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Projectionist |
49
|
|
|
*/ |
50
|
|
|
private $projectionist; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Creates a Projection Event Listener |
54
|
|
|
* |
55
|
|
|
* @param array $projectors |
56
|
|
|
* @param EventHandlingStrategy $strategy |
57
|
|
|
* @param ProjectorStateLedger $ledger |
58
|
|
|
*/ |
59
|
|
|
public function __construct(array $projectors, EventHandlingStrategy $strategy, ProjectorStateLedger $ledger) |
60
|
|
|
{ |
61
|
|
|
$this->projectors = $projectors; |
62
|
|
|
$this->strategy = $strategy; |
63
|
|
|
$this->ledger = $ledger; |
64
|
|
|
|
65
|
|
|
$this->eventStore = new ListenerEventStore(); |
66
|
|
|
$this->projectionist = new Projectionist($this->eventStore, $ledger, $strategy); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Handle an event. |
71
|
|
|
* |
72
|
|
|
* @param EventInterface|Event $event |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
* @throws \Exception |
76
|
|
|
*/ |
77
|
|
|
public function handle(EventInterface $event) |
78
|
|
|
{ |
79
|
|
|
$this->eventStore->append(StoredEvent::createFromEvent($event)); |
80
|
|
|
$this->projectionist->play($this->projectors); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|