Completed
Push — master ( 91b636...82125a )
by Freek
01:52
created

ChooseSnapshot::displaySnapshots()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 1
1
<?php
2
3
namespace Spatie\EventProjector\Console\Snapshots\Concerns;
4
5
use Illuminate\Support\Collection;
6
use Spatie\EventProjector\Snapshots\Snapshot;
7
8
trait ChooseSnapshot
9
{
10
    public function chooseSnapshot(string $question, Collection $snapshots): ?Snapshot
11
    {
12
        if ($snapshots->isEmpty()) {
13
            $this->warn("There currently are no snapshots. You can take a snapshot by running `php artisan event-projector:create-snapshot`.");
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...
14
15
            return null;
16
        }
17
18
        $this->displaySnapshots($snapshots);
19
20
        $snapshotNumber = $this->ask($question);
0 ignored issues
show
Bug introduced by
It seems like ask() 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...
21
22
        if (! $snapshot = $snapshots->get($snapshotNumber - 1)) {
23
            $this->error("There is no snapshot for that number.");
0 ignored issues
show
Bug introduced by
It seems like error() 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...
24
25
            return null;
26
        }
27
28
        return $snapshot;
29
    }
30
31
    public function displaySnapshots(Collection $snapshots)
32
    {
33
        $titles = ['Number', 'Projector', 'Last processed event id', 'Created at', 'Name'];
34
35
        $rows = $snapshots->map(function (Snapshot $snapshot, int $index) {
36
            return [
37
                $index + 1,
38
                $snapshot->projectorName(),
39
                $snapshot->lastProcessedEventId(),
40
                $snapshot->createdAt(),
41
                $snapshot->name(),
42
            ];
43
        });
44
45
        $this->table($titles, $rows);
0 ignored issues
show
Bug introduced by
It seems like table() 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...
46
    }
47
}