Completed
Push — master ( 642b9b...581e49 )
by Freek
02:21
created

ReplaysEvents::getStoredEventClass()   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 0
1
<?php
2
3
namespace Spatie\EventProjector\Console\Concerns;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Facades\DB;
7
use Spatie\EventProjector\Models\StoredEvent;
8
use Spatie\EventProjector\Projectors\Projector;
9
use Spatie\EventProjector\Models\ProjectorStatus;
10
11
trait ReplaysEvents
12
{
13
    public function replay(Collection $projectors)
14
    {
15
        $afterEventId = $this->determineAfterEventId($projectors);
16
17
        if ($afterEventId === $this->getStoredEventClass()::getMaxId()) {
0 ignored issues
show
Bug introduced by
The method getMaxId cannot be called on $this->getStoredEventClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
18
            $this->warn('There are no events to replay.');
0 ignored issues
show
Bug introduced by
It seems like warn() 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...
19
        }
20
21
        $replayCount = $this->getStoredEventClass()::after($afterEventId)->count();
0 ignored issues
show
Bug introduced by
The method after cannot be called on $this->getStoredEventClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
22
23
        if ($replayCount === 0) {
24
            $this->warn('There are no events to replay');
0 ignored issues
show
Bug introduced by
It seems like warn() 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...
25
26
            return;
27
        }
28
29
        $afterEventId === 0
30
            ? $this->comment('Replaying all events...')
0 ignored issues
show
Bug introduced by
It seems like comment() 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...
31
            : $this->comment("Replaying events after stored event id {$afterEventId}...");
0 ignored issues
show
Bug introduced by
It seems like comment() 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...
32
        $this->emptyLine();
33
34
        $bar = $this->output->createProgressBar($this->getStoredEventClass()::after($afterEventId)->count());
0 ignored issues
show
Bug introduced by
The property output 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...
Bug introduced by
The method after cannot be called on $this->getStoredEventClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
35
        $onEventReplayed = function () use ($bar) {
36
            $bar->advance();
37
        };
38
39
        $this->projectionist->replay($projectors, $afterEventId, $onEventReplayed);
0 ignored issues
show
Bug introduced by
The property projectionist 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...
40
41
        $bar->finish();
42
43
        $this->emptyLine(2);
44
        $this->comment('All done!');
0 ignored issues
show
Bug introduced by
It seems like comment() 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...
45
    }
46
47
    protected function determineAfterEventId(Collection $projectors): int
48
    {
49
        $projectorsWithoutStatus = collect($projectors)
50
            ->filter(function (Projector $projector) {
51
                return !ProjectorStatus::query()
0 ignored issues
show
Bug introduced by
The method exists() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean canUseExistsForExistenceCheck()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
52
                    ->where('projector_name', $projector->getName())
53
                    ->exists();
54
            });
55
56
        if ($projectorsWithoutStatus->isNotEmpty()) {
57
            return 0;
58
        }
59
60
        $allProjectorStatusesCount = DB::table('projector_statuses')
61
            ->whereIn('projector_name', $projectors->map->getName()->toArray())
62
            ->count();
63
64
        $allUpToDateProjectorStatusesCount = DB::table('projector_statuses')
65
            ->whereIn('projector_name', $projectors->map->getName()->toArray())
66
            ->where('has_received_all_events', true)
67
            ->count();
68
69
        if ($allProjectorStatusesCount === $allUpToDateProjectorStatusesCount) {
70
            return $this->getStoredEventClass()::getMaxId();
0 ignored issues
show
Bug introduced by
The method getMaxId cannot be called on $this->getStoredEventClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
71
        }
72
73
        return DB::table('projector_statuses')
74
                ->whereIn('projector_name', $projectors->map->getName()->toArray())
75
                ->where('has_received_all_events', false)
76
                ->min('last_processed_event_id') ?? 0;
77
    }
78
79
    protected function emptyLine(int $amount = 1)
80
    {
81
        foreach (range(1, $amount) as $i) {
82
            $this->line('');
0 ignored issues
show
Bug introduced by
It seems like line() 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...
83
        }
84
    }
85
86
    protected function getStoredEventClass(): string
87
    {
88
        return config('event-projector.stored_event_model');
89
    }
90
}
91