Completed
Push — master ( 75cf25...763608 )
by Freek
06:48
created

DeleteSnapshotCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Spatie\EventProjector\Console\Snapshots;
4
5
use Illuminate\Console\Command;
6
7
class DeleteSnapshotCommand extends Command
8
{
9
    protected $signature = 'event-projector:delete-snapshot';
10
11
    protected $description = 'Delete snapshots';
12
13
    /** @var \Illuminate\Support\Collection */
14
    protected  $snapshots;
15
16
    public function __construct()
17
    {
18
        $this->snapshots = $this->snapshotRepository->get();
0 ignored issues
show
Bug introduced by
The property snapshotRepository 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...
19
    }
20
21
    public function handle()
22
    {
23
        if ($this->snapshots->isEmpty()) {
24
            $this->warn("There currently are no snapshots. You can take a snapshot by running `php artisan event-projector:create-snapshot`.");
25
26
            return;
27
        }
28
29
        $this->displaySnapshots();
30
31
        $snapshotNumber = $this->ask('Which snapshot would you like to delete?');
32
33
        if (! $snapshot = $this->snapshots->get($snapshotNumber)) {
34
            $this->error("There is no snapshot for that number.");
35
36
            return;
37
        }
38
39
        $snapshot->delete();
40
41
        $this->comment("Snapshot number {$snapshotNumber} deleted!");
42
    }
43
44 View Code Duplication
    public function displaySnapshots()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        $titles = ['Number', 'Projector', 'Last processed event id', 'Created at', 'Name'];
47
48
        $rows = $this->snapshots->map(function (Snapshot $snapshot, int $index) {
49
            return [
50
                $index,
51
                $snapshot->projectorName(),
52
                $snapshot->lastProcessedEventId(),
53
                $snapshot->createdAt(),
54
                $snapshot->name(),
55
            ];
56
        });
57
58
        $this->table($titles, $rows);
59
    }
60
}
61