Repository navigation
Expand file tree
/
Copy pathUpdateOrCreateUser.php
More file actions
69 lines (57 loc) · 2.41 KB
/
Copy pathUpdateOrCreateUser.php
File metadata and controls
69 lines (57 loc) · 2.41 KB
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
65
66
67
68
69
<?php
declare(strict_types=1);
namespace App\Actions;
use App\Contracts\Services\GitHubProvider;
use App\Dto\GitHubProfile;
use App\Enums\ProfileType;
use App\Models\Profile;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Laravel\Socialite\Two\User as GitHubUser;
final readonly class UpdateOrCreateUser
{
public function __construct(
private GitHubProvider $github,
private UpdateStaleHandles $updateStaleHandles,
private CreateFollowsFromPendingFollows $createFollowsFromPendingFollows,
) {
//
}
/**
* Signing in makes the GitHub account's profile a user's: an unclaimed one, looked up from GitHub before, becomes
* theirs with its ID, and the members waiting for them follow them. Someone who deleted their account and comes back
* is no longer remembered as gone.
*/
public function handle(GitHubUser $githubUser): User
{
$githubId = (string) $githubUser->getId();
$profile = GitHubProfile::fromSocialite($githubUser);
$email = mb_trim((string) $githubUser->getEmail());
// Null when GitHub can't answer, which keeps the last known value.
$hasSponsorsListing = $this->github->hasSponsorsListing($githubUser->token);
return DB::transaction(function () use ($githubId, $profile, $email, $hasSponsorsListing): User {
$this->updateStaleHandles->handle($profile->handle, $githubId);
// A profile never changes type, so an organization's GitHub ID would fail here, but GitHub never signs one in...
$id = Profile::query()->updateOrCreate([
'github_id' => $githubId,
'type' => ProfileType::User,
], [
...$profile->attributes(),
'github_synced_at' => now(),
])->id;
DB::table('deleted_github_accounts')->where('github_id', $githubId)->delete();
$user = User::query()->updateOrCreate([
'id' => $id,
'github_id' => $githubId,
], [
'company' => $profile->company,
'email' => $email === '' ? null : $email,
...($hasSponsorsListing === null ? [] : ['has_sponsors_listing' => $hasSponsorsListing]),
]);
if ($user->wasRecentlyCreated) {
$this->createFollowsFromPendingFollows->handle($user);
}
return $user;
});
}
}