/
Users.php
92 lines (77 loc) · 2.57 KB
/
Users.php
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace Laravel\Pulse;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Collection;
use Laravel\Pulse\Contracts\ResolvesUsers;
class Users implements ResolvesUsers
{
/**
* The resolved users.
*
* @var Collection<int, \Illuminate\Contracts\Auth\Authenticatable>
*/
protected Collection $resolvedUsers;
/**
* The field resolver.
*
* @var ?callable(\Illuminate\Contracts\Auth\Authenticatable): object{name: string, extra?: string, avatar?: string}
*/
protected $fieldResolver = null;
/**
* Return a unique key identifying the user.
*/
public function key(Authenticatable $user): int|string|null
{
return $user->getAuthIdentifier();
}
/**
* Eager load the users with the given keys.
*
* @param Collection<int, int|string|null> $keys
*/
public function load(Collection $keys): self
{
$auth = app('auth');
$provider = $auth->createUserProvider(
config("auth.guards.{$auth->getDefaultDriver()}.provider")
);
if ($provider instanceof EloquentUserProvider) {
$model = $provider->getModel();
$this->resolvedUsers = $model::findMany($keys);
} else {
$this->resolvedUsers = $keys->map(fn ($key) => $provider->retrieveById($key));
}
return $this;
}
/**
* Find the user with the given key.
*
* @return object{name: string, extra?: string, avatar?: string}
*/
public function find(int|string|null $key): object
{
$user = $this->resolvedUsers->first(fn ($user) => $this->key($user) == $key);
if ($this->fieldResolver !== null && $user !== null) {
return (object) ($this->fieldResolver)($user);
}
return (object) [
'name' => $user->name ?? "ID: $key",
'extra' => $user->email ?? '',
'avatar' => $user->avatar ?? (($user->email ?? false)
? sprintf('https://gravatar.com/avatar/%s?d=mp', hash('sha256', trim(strtolower($user->email)))) // @phpstan-ignore property.nonObject
: sprintf('https://gravatar.com/avatar?d=mp')
),
];
}
/**
* Override the field resolver.
*
* @param callable(\Illuminate\Contracts\Auth\Authenticatable): object{name: string, extra?: string, avatar?: string} $resolver
*/
public function setFieldResolver(callable $resolver): self
{
$this->fieldResolver = $resolver;
return $this;
}
}