-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCleanupRegistration.php
64 lines (53 loc) · 1.55 KB
/
CleanupRegistration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace App\Console\Commands;
use App\Models\Event;
use App\Models\User;
use Illuminate\Console\Command;
class CleanupRegistration extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'registration:cleanup-event';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle(): void
{
$events = Event::all(['id', 'name'])->toArray();
if (count($events) == 0) {
$this->info("There are no events to clean up. You're all good.");
return;
}
$this->question('Which of the following events should be cleaned up?');
$this->table(['ID', 'Name'], $events, 'box');
$id = $this->ask('Id of the event to be cleaned up?');
$this->cleanUpEvent($id);
}
private function cleanUpEvent(mixed $id): void
{
/** @var Event|null $event */
$event = Event::find($id);
if ($event == null) {
$this->warn("Did not find an event with id '".$id."'");
return;
}
$event->attendees()->each(function ($attendee): bool {
if ($attendee instanceof User) {
if ($attendee->events->count() == 1 && ! $attendee->hasRole('rotex')) {
$attendee->delete();
}
}
return true;
});
$event->delete();
}
}