|
5 | 5 | use Illuminate\Console\Command;
|
6 | 6 | use App\Models\User;
|
7 | 7 | use Illuminate\Database\Eloquent\Collection;
|
8 |
| -use Illuminate\Support\Facades\Http; |
| 8 | +use App\Social\GithubUserApi; |
9 | 9 |
|
10 |
| -class RemoveDuplicateGithubUsernames extends Command |
| 10 | +class ResolveDuplicateGithubUsernames extends Command |
11 | 11 | {
|
12 |
| - protected $signature = 'lio:remove-duplicate-github-usernames'; |
| 12 | + protected $signature = 'lio:resolve-duplicate-github-usernames'; |
13 | 13 |
|
14 |
| - protected $description = 'Removes duplicate Github usernames from the database'; |
| 14 | + protected $description = 'Resolve duplicate GitHub usernames from the database'; |
| 15 | + |
| 16 | + public function __construct( |
| 17 | + private readonly GithubUserApi $github |
| 18 | + ){ |
| 19 | + parent::__construct(); |
| 20 | + } |
15 | 21 |
|
16 | 22 | public function handle(): void
|
17 | 23 | {
|
18 |
| - $this->info('Removing duplicate Github usernames...'); |
| 24 | + $this->info('Resolving duplicate Github usernames...'); |
19 | 25 |
|
20 |
| - $this->removeDuplicates(); |
| 26 | + $this->resolveDuplicates(); |
21 | 27 |
|
22 |
| - $this->info('Duplicate Github usernames removed!'); |
| 28 | + $this->info('Duplicate Github usernames resolved!'); |
23 | 29 | }
|
24 | 30 |
|
25 |
| - private function removeDuplicates(): void |
| 31 | + /** |
| 32 | + * Resolve duplicate github_username |
| 33 | + */ |
| 34 | + private function resolveDuplicates(): void |
26 | 35 | {
|
27 | 36 | $this
|
28 | 37 | ->duplicates()
|
29 | 38 | ->groupBy('github_username')
|
30 |
| - ->each(function (Collection $duplicates) { |
| 39 | + ->each(function (Collection $users) { |
31 | 40 |
|
32 | 41 | $uniqueUsernames = [];
|
33 | 42 |
|
34 |
| - foreach($duplicates as $user) { |
| 43 | + // order from the latest user to the oldest |
| 44 | + $users = $users->sortByDesc('created_at'); |
35 | 45 |
|
36 |
| - // set github_username to null if the user has no github_id |
37 |
| - if (! $user->github_id) { |
38 |
| - $user->update(['github_username' => null]); |
39 |
| - continue; |
40 |
| - } |
| 46 | + // resolve each user with the same github_username |
| 47 | + foreach($users as $user) { |
41 | 48 |
|
42 |
| - // fetch the user from GitHub |
43 |
| - $githubUser = $this->fetchFromGithub($user); |
| 49 | + // fetch the user from GitHub API |
| 50 | + $githubUser = $this->github->find($user->github_id); |
44 | 51 |
|
45 |
| - // if the user doesn't exist on GitHub, set github_username to null |
| 52 | + // if the user doesn't exist on GitHub |
46 | 53 | if (! $githubUser || ! $githubUser->login) {
|
47 |
| - $user->update(['github_username' => null, 'github_id' => null]); |
| 54 | + $user->update(['github_username' => null]); |
48 | 55 | continue;
|
49 | 56 | }
|
50 | 57 |
|
51 |
| - // if the user's github_username marked as unique, set github_username to null |
| 58 | + // if the user's github_username marked as unique |
52 | 59 | if (in_array($githubUser->login, $uniqueUsernames)) {
|
53 |
| - //TODO: delete user? |
54 |
| - $user->update(['github_username' => null, 'github_id' => null]); |
| 60 | + $user->update(['github_username' => null]); |
55 | 61 | continue;
|
56 | 62 | }
|
57 | 63 |
|
58 | 64 | // if the user exists on GitHub, update the user's github_username
|
59 | 65 | $user->update(['github_username' => $githubUser->login]);
|
60 | 66 |
|
61 |
| - // add the user's github_username to the uniqueUsernames array |
| 67 | + // mark the user's github_username as unique |
62 | 68 | $uniqueUsernames[] = $githubUser->login;
|
63 | 69 | }
|
64 | 70 | });
|
65 | 71 | }
|
66 | 72 |
|
67 | 73 | /**
|
68 |
| - * @param User $user |
69 |
| - * @return object|null |
70 |
| - */ |
71 |
| - private function fetchFromGithub(User $user): ?object |
72 |
| - { |
73 |
| - $response = Http::get("https://api.github.com/user/{$user->github_id}"); |
74 |
| - |
75 |
| - if ($response->failed()) { |
76 |
| - return null; |
77 |
| - } |
78 |
| - |
79 |
| - return $response->object(); |
80 |
| - } |
81 |
| - |
82 |
| - /** |
| 74 | + * Get all users with duplicate github_username |
83 | 75 | * @return Collection
|
84 | 76 | */
|
85 | 77 | private function duplicates(): Collection
|
|
0 commit comments