Completed
Push — master ( f318b1...b9f34b )
by Freek
05:03 queued 43s
created

EventSubscriber::handleEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Spatie\EventProjector;
4
5
class EventSubscriber
6
{
7
    /** @var \Spatie\EventProjector\Projectionist */
8
    protected $projectionist;
9
10
    /** @var array */
11
    protected $config;
12
13
    public function __construct(Projectionist $projectionist, array $config)
14
    {
15
        $this->projectionist = $projectionist;
16
17
        $this->config = $config;
18
    }
19
20
    public function subscribe($events)
21
    {
22
        $events->listen('*', static::class.'@handle');
23
    }
24
25
    public function handle(string $eventName, $payload)
26
    {
27
        if (! $this->shouldBeStored($eventName)) {
28
            return;
29
        }
30
31
        $this->storeEvent($payload[0]);
32
    }
33
34
    public function storeEvent(ShouldBeStored $event)
35
    {
36
        $this->projectionist->storeEvent($event);
37
    }
38
39
    protected function shouldBeStored($event): bool
40
    {
41
        if (! class_exists($event)) {
42
            return false;
43
        }
44
45
        return is_subclass_of($event, ShouldBeStored::class);
46
    }
47
}
48