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 App\Libraries\User;
9
10use App\Exceptions\InvariantException;
11use App\Models\Beatmap;
12use App\Models\User;
13use App\Models\UserAccountHistory;
14
15class CountryChange
16{
17 public static function handle(User $user, string $newCountry, string $reason): void
18 {
19 // Assert valid country acronym
20 $country = app('countries')->byCode($newCountry);
21 if ($country === null) {
22 throw new InvariantException('invalid country specified');
23 }
24
25 if ($user->country_acronym === $newCountry) {
26 return;
27 }
28
29 $user->getConnection()->transaction(function () use ($newCountry, $reason, $user) {
30 $oldCountry = $user->country_acronym;
31
32 $newAttrs = ['country_acronym' => $newCountry];
33 $user->update($newAttrs);
34 foreach (Beatmap::MODES as $ruleset => $_rulesetId) {
35 $user->statistics($ruleset, true)->update($newAttrs);
36 $user->scoresBest($ruleset, true)->update($newAttrs);
37 foreach (Beatmap::VARIANTS[$ruleset] ?? [] as $variant) {
38 $user->statistics($ruleset, true, $variant)->update($newAttrs);
39 }
40 }
41
42 UserAccountHistory::addNote($user, "Changing country from {$oldCountry} to {$newCountry} ({$reason})");
43 });
44
45 \Artisan::queue('es:index-scores:queue', [
46 '--all' => true,
47 '--no-interaction' => true,
48 '--user' => $user->getKey(),
49 ]);
50 }
51}