1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6declare(strict_types=1);
7
8namespace Database\Factories;
9
10use App\Models\Beatmap;
11use App\Models\Group;
12use App\Models\User;
13use App\Models\UserGroupEvent;
14
15class UserGroupEventFactory extends Factory
16{
17 protected $model = UserGroupEvent::class;
18
19 public function configure(): static
20 {
21 // Fill in details after making the Model so that the caller can set
22 // their own details without overwriting the entire array.
23 return $this->afterMaking(function (UserGroupEvent $event) {
24 $defaultDetails = [
25 'actor_name' => $event->actor?->username,
26 'group_name' => $event->group->group_name,
27 'user_name' => $event->user?->username,
28 ];
29
30 switch ($event->type) {
31 case UserGroupEvent::GROUP_RENAME:
32 $defaultDetails['previous_group_name'] = "Old {$event->group->group_name}";
33 break;
34
35 case UserGroupEvent::USER_ADD:
36 case UserGroupEvent::USER_ADD_PLAYMODES:
37 case UserGroupEvent::USER_REMOVE_PLAYMODES:
38 $defaultDetails['playmodes'] = [array_rand(Beatmap::MODES)];
39 break;
40 }
41
42 $event->details = array_merge($defaultDetails, $event->details);
43 });
44 }
45
46 public function definition(): array
47 {
48 return [
49 'actor_id' => User::factory(),
50 'details' => [],
51 'group_id' => Group::factory(),
52 'hidden' => false,
53 'type' => fn () => $this->faker->randomElement([
54 UserGroupEvent::GROUP_ADD,
55 UserGroupEvent::GROUP_REMOVE,
56 UserGroupEvent::GROUP_RENAME,
57 UserGroupEvent::USER_ADD,
58 UserGroupEvent::USER_ADD_PLAYMODES,
59 UserGroupEvent::USER_REMOVE,
60 UserGroupEvent::USER_REMOVE_PLAYMODES,
61 UserGroupEvent::USER_SET_DEFAULT,
62 ]),
63
64 // depends on type
65 'user_id' => fn (array $attr) => match ($attr['type']) {
66 UserGroupEvent::GROUP_ADD,
67 UserGroupEvent::GROUP_REMOVE,
68 UserGroupEvent::GROUP_RENAME => null,
69
70 default => User::factory(),
71 },
72 ];
73 }
74}