the browser-facing portion of osu!
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\Chat;
9
10use App\Exceptions\InvariantException;
11use App\Models\Chat\Channel;
12use App\Models\LegacyMatch\LegacyMatch;
13use App\Models\User;
14use Database\Factories\Factory;
15
16class ChannelFactory extends Factory
17{
18 protected $model = Channel::class;
19
20 public function definition(): array
21 {
22 return [
23 'name' => '#'.$this->faker->colorName(),
24 'description' => $this->faker->bs(),
25 ];
26 }
27
28 public function moderated(): static
29 {
30 return $this->state(['moderated' => true]);
31 }
32
33 public function pm(User ...$users): static
34 {
35 if (empty($users)) {
36 $users = User::factory()->count(2)->create();
37 }
38
39 if (count($users) !== 2) {
40 throw new InvariantException('Creating PM Channels requires 2 users.');
41 }
42
43 return $this->state([
44 'name' => Channel::getPMChannelName(...$users),
45 'type' => Channel::TYPES['pm'],
46 'description' => '',
47 ])->withUsers(...$users);
48 }
49
50 public function tourney(): static
51 {
52 $match = LegacyMatch::factory()->tourney()->create();
53
54 return $this->state([
55 'name' => "#mp_{$match->getKey()}",
56 'type' => Channel::TYPES['temporary'],
57 ]);
58 }
59
60 public function type(string $type, array $users = []): static
61 {
62 if ($type === 'tourney') {
63 return $this->tourney();
64 } elseif ($type === 'pm') {
65 return $this->pm(...$users);
66 }
67
68 return $this->state(['type' => Channel::TYPES[$type]])->withUsers(...$users);
69 }
70
71 public function withUsers(User ...$users): static
72 {
73 return $this->afterCreating(function (Channel $channel) use ($users) {
74 foreach ($users as $user) {
75 $channel->addUser($user);
76 }
77 });
78 }
79}