ProjectionEventListener::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
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