Completed
Push — master ( 0157de...6fef55 )
by David
02:27
created

EventPublisherMixin::publishAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Rawkode\Eidetic\EventStore;
4
5
use Rawkode\Eidetic\EventSourcing\EventSourcedEntity;
6
7
trait EventPublisherMixin
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $subscribers = [];
13
14
    /**
15
     * @param Subscriber $subscriber
16
     */
17 2
    public function registerSubscriber($subscriber)
18
    {
19 2
        array_push($this->subscribers, $subscriber);
20 2
    }
21
22
    /**
23
     * @param string             $eventHook
24
     * @param EventSourcedEntity $eventSourcedEntity
25
     */
26 14
    public function publishAll($eventHook, EventSourcedEntity $eventSourcedEntity)
27
    {
28 14
        foreach ($eventSourcedEntity->stagedEvents() as $event) {
29 14
            $this->publish($eventHook, $eventSourcedEntity, $event);
30 14
        }
31 14
    }
32
33
    /**
34
     * @param int                $eventHook
35
     * @param EventSourcedEntity $eventSourcedEntity
36
     * @param object             $event
37
     */
38 14
    public function publish($eventHook, EventSourcedEntity $eventSourcedEntity, $event)
39
    {
40
        /** @var Subscriber $subscriber */
41 14
        foreach ($this->subscribers as $subscriber) {
42 2
            $subscriber->handle($eventHook, $eventSourcedEntity, $event);
43 14
        }
44 14
    }
45
}
46