EventPublisherMixin   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 39
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1
ccs 13
cts 13
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerSubscriber() 0 4 1
A publishAll() 0 6 2
A publish() 0 7 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