Completed
Push — master ( 06db97...7bf108 )
by Freek
04:52
created

ProjectsEvents::groupProjectorStatusBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
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 rememberReceivedEvent(StoredEvent $storedEvent)
26
    {
27
        $streamFullNames = collect($this->streamEventsBy($storedEvent))
28
            ->map(function ($streamValue, $streamName) {
29
                return "{$streamName}-{$streamValue}";
30
            })
31
            ->toArray();
32
33
        if (count($streamFullNames) === 0) {
34
            $streamFullNames = ['main'];
35
        }
36
37
        foreach ($streamFullNames as $streamName) {
38
            $this->getStatus($streamName)->rememberLastProcessedEvent($storedEvent, $this);
39
        }
40
    }
41
42
    public function hasReceivedAllPriorEvents(StoredEvent $storedEvent): bool
43
    {
44
        $streams = collect($this->streamEventsBy($storedEvent));
45
46
        if ($streams->isEmpty()) {
47
            $lastStoredEvent = StoredEvent::query()
48
                ->whereIn('event_class', $this->handlesEventClassNames())
49
                ->where('id', '<', $storedEvent->id)
50
                ->orderBy('id', 'desc')
51
                ->first();
52
53
            $lastStoredEventId = (int) optional($lastStoredEvent)->id ?? 0;
54
55
            $lastProcessedEventId = (int) $this->getStatus()->last_processed_event_id ?? 0;
56
57
            return $lastStoredEventId === $lastProcessedEventId;
58
        }
59
60
        foreach ($streams as $streamName => $streamValue) {
61
            $streamFullName = "{$streamName}-{$streamValue}";
62
            $whereJsonClause = str_replace('.', '->', $streamName);
63
64
            $lastStoredEvent = StoredEvent::query()
65
                ->whereIn('event_class', $this->handlesEventClassNames())
66
                ->where('id', '<', $storedEvent->id)
67
                ->where("event_properties->{$whereJsonClause}", $streamValue)
68
                ->orderBy('id', 'desc')
69
                ->first();
70
71
            $lastStoredEventId = (int) optional($lastStoredEvent)->id ?? 0;
72
73
            $lastProcessedEventId = (int) $this->getStatus($streamFullName)->last_processed_event_id ?? 0;
74
75
            if ($lastStoredEventId !== $lastProcessedEventId) {
76
                return false;
77
            }
78
        }
79
80
        return true;
81
    }
82
83
    public function hasReceivedAllEvents(): bool
84
    {
85
        return ProjectorStatus::hasReceivedAllEvents($this);
86
    }
87
88
    public function getLastProcessedEventId(): int
89
    {
90
        return $this->getStatus()->last_processed_event_id ?? 0;
91
    }
92
93
    public function lastEventProcessedAt(): Carbon
94
    {
95
        return $this->getStatus()->updated_at;
96
    }
97
98
    public function reset()
99
    {
100
        if (! method_exists($this, 'resetState')) {
101
            throw CouldNotResetProjector::doesNotHaveResetStateMethod($this);
102
        }
103
104
        $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...
105
106
        $this->getAllStatuses()->each->delete();
107
    }
108
109
    public function shouldBeCalledImmediately(): bool
110
    {
111
        return ! $this instanceof QueuedProjector;
112
    }
113
114
    public function streamEventsBy(StoredEvent $storedEvent): array
0 ignored issues
show
Unused Code introduced by
The parameter $storedEvent is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
115
    {
116
        return [];
117
    }
118
119
    protected function getStatus(string $stream = 'main'): ProjectorStatus
120
    {
121
        return ProjectorStatus::getForProjector($this, $stream);
122
    }
123
124
    protected function getAllStatuses(): Collection
125
    {
126
        return ProjectorStatus::getAllForProjector($this);
127
    }
128
}
129