EventSubscriber   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 39
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 handle() 0 8 2
A storeEvent() 0 5 1
A shouldBeStored() 0 8 2
1
<?php
2
3
namespace Spatie\EventProjector;
4
5
final class EventSubscriber
6
{
7
    /** @var \Spatie\EventProjector\StoredEventRepository */
8
    private $repository;
9
10
    public function __construct(string $storedEventRepository)
11
    {
12
        $this->repository = app($storedEventRepository);
13
    }
14
15
    public function subscribe($events): void
16
    {
17
        $events->listen('*', static::class.'@handle');
18
    }
19
20
    public function handle(string $eventName, $payload): void
21
    {
22
        if (! $this->shouldBeStored($eventName)) {
23
            return;
24
        }
25
26
        $this->storeEvent($payload[0]);
27
    }
28
29
    public function storeEvent(ShouldBeStored $event): void
30
    {
31
        $storedEvent = $this->repository->persist($event);
32
        $storedEvent->handle();
33
    }
34
35
    private function shouldBeStored($event): bool
36
    {
37
        if (! class_exists($event)) {
38
            return false;
39
        }
40
41
        return is_subclass_of($event, ShouldBeStored::class);
42
    }
43
}
44