Completed
Push — master ( 0ba1f3...e49bb9 )
by Freek
03:02
created

EventSubscriber   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 42
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A subscribe() 0 4 1
A handleEvent() 0 8 2
A storeEvent() 0 8 1
A shouldBeStored() 0 8 2
1
<?php
2
3
namespace Spatie\EventProjector;
4
5
class EventSubscriber
6
{
7
    /** @var \Spatie\EventProjector\EventProjectionist */
8
    protected $evenSorcerer;
9
10
    public function __construct(EventProjectionist $evenSorcerer)
11
    {
12
        $this->evenSorcerer = $evenSorcerer;
13
    }
14
15
    public function subscribe($events)
16
    {
17
        $events->listen('*', static::class.'@handleEvent');
18
    }
19
20
    public function handleEvent(string $eventName, $payload)
21
    {
22
        if (! $this->shouldBeStored($eventName)) {
23
            return;
24
        }
25
26
        $this->storeEvent($payload[0]);
27
    }
28
29
    public function storeEvent(ShouldBeStored $event)
30
    {
31
        StoredEvent::createForEvent($event);
32
33
        $this->evenSorcerer
34
            ->callEventHandlers($this->evenSorcerer->mutators, $event)
35
            ->callEventHandlers($this->evenSorcerer->reactors, $event);
36
    }
37
38
    protected function shouldBeStored($event): bool
39
    {
40
        if (! class_exists($event)) {
41
            return false;
42
        }
43
44
        return is_subclass_of($event, ShouldBeStored::class);
45
    }
46
}
47