Completed
Push — master ( c92ba1...102b41 )
by Freek
04:51
created

ProjectsEvents::rememberReceivedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Spatie\EventProjector\Projectors;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Collection;
7
use Spatie\EventProjector\Models\StoredEvent;
8
use Spatie\EventProjector\Models\ProjectorStatus;
9
use Spatie\EventProjector\EventHandlers\HandlesEvents;
10
use Spatie\EventProjector\Exceptions\CouldNotResetProjector;
11
12
trait ProjectsEvents
13
{
14
    use HandlesEvents;
15
16
    public function getName(): string
17
    {
18
        if (isset($this->name)) {
19
            return $this->name;
0 ignored issues
show
Bug introduced by
The property name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
        }
21
22
        return get_class($this);
23
    }
24
25
    public function streamNamesToTrack(): array
26
    {
27
        return array_wrap($this->trackStream ?? []);
0 ignored issues
show
Bug introduced by
The property trackStream does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
    }
29
30
    public function trackEventsByStreamNameAndId(): bool
31
    {
32
        return count($this->streamNamesToTrack()) === 0;
33
    }
34
35
    public function handlesStreamOfStoredEvent(StoredEvent $storedEvent): bool
36
    {
37
        $trackedStreamNames = $this->streamNamesToTrack();
38
39
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
40
        if ($trackedStreamNames === []) {
41
            return true;
42
        }
43
        */
44
45
        $event = $storedEvent->event;
46
47
        $streamNameOfEvent = method_exists($event, 'getStreamName')
48
            ? $event->getStreamName()
49
            : 'main';
50
51
        if (in_array('*', $trackedStreamNames)) {
52
            return true;
53
        }
54
55
        return in_array($streamNameOfEvent, $trackedStreamNames);
56
    }
57
58
    public function rememberReceivedEvent(StoredEvent $storedEvent)
59
    {
60
        $this->getStatus($storedEvent)->rememberLastProcessedEvent($storedEvent, $this);
61
    }
62
63
    public function hasReceivedAllPriorEvents(StoredEvent $storedEvent): bool
64
    {
65
        if (! $this->trackEventsByStreamNameAndId()) {
66
            return $storedEvent->id === $this->getStatus()->last_processed_event_id + 1;
67
        }
68
69
        $previousEvent = $storedEvent->previousInStream();
70
        $previousEventId = optional($previousEvent)->id ?? 0;
71
72
        $lastProcessedEventId = (int) $this->getStatus($storedEvent)->last_processed_event_id ?? 0;
73
74
        return $previousEventId === $lastProcessedEventId;
75
    }
76
77
    public function hasReceivedAllEvents(): bool
78
    {
79
        return ProjectorStatus::hasReceivedAllEvents($this);
80
    }
81
82
    public function getLastProcessedEventId(): int
83
    {
84
        return $this->getStatus()->last_processed_event_id ?? 0;
85
    }
86
87
    public function lastEventProcessedAt(): Carbon
88
    {
89
        return $this->getStatus()->updated_at;
90
    }
91
92
    public function reset()
93
    {
94
        if (! method_exists($this, 'resetState')) {
95
            throw CouldNotResetProjector::doesNotHaveResetStateMethod($this);
96
        }
97
98
        $this->resetState();
0 ignored issues
show
Bug introduced by
It seems like resetState() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
99
100
        $this->getAllStatuses()->each->delete();
101
    }
102
103
    public function shouldBeCalledImmediately(): bool
104
    {
105
        return ! $this instanceof QueuedProjector;
106
    }
107
108
    protected function getStatus(StoredEvent $storedEvent = null): ProjectorStatus
109
    {
110
        return ProjectorStatus::getForProjector($this, $storedEvent);
111
    }
112
113
    protected function getAllStatuses(): Collection
114
    {
115
        return ProjectorStatus::getAllForProjector($this);
116
    }
117
}
118