1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Xetaravel\Models\Repositories; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Support\Facades\Request as FacadeRequest; |
9
|
|
|
use Xetaravel\Models\User; |
10
|
|
|
use DateTimeImmutable; |
11
|
|
|
|
12
|
|
|
class UserRepository |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Find the authors of articles with most articles for the sidebar. |
16
|
|
|
* |
17
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
18
|
|
|
*/ |
19
|
|
|
public static function sidebar(): Collection |
20
|
|
|
{ |
21
|
|
|
return User::where('blog_article_count', '>=', 1) |
22
|
|
|
->take(config('xetaravel.blog.users_sidebar')) |
23
|
|
|
->orderBy('blog_article_count', 'desc') |
24
|
|
|
->get(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create a new user instance after a valid registration. |
29
|
|
|
* |
30
|
|
|
* @param array $data The data used to create the user. |
31
|
|
|
* @param array $providerData The additional data provided by the provider. |
32
|
|
|
* @param bool $provider Whether the user is registered with a Social Provider. |
33
|
|
|
* |
34
|
|
|
* @return User |
35
|
|
|
*/ |
36
|
|
|
public static function create(array $data, array $providerData = [], bool $provider = false): User |
37
|
|
|
{ |
38
|
|
|
$ip = FacadeRequest::ip(); |
39
|
|
|
|
40
|
|
|
$user = [ |
41
|
|
|
'username' => $data['username'], |
42
|
|
|
'email' => $data['email'], |
43
|
|
|
'register_ip' => $ip, |
44
|
|
|
'last_login_ip' => $ip, |
45
|
|
|
'last_login_date' => new DateTimeImmutable() |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
if ($provider === false) { |
49
|
|
|
$user += [ |
50
|
|
|
'password' => bcrypt($data['password']) |
51
|
|
|
]; |
52
|
|
|
} else { |
53
|
|
|
$user += $providerData; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return User::create($user); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Update the user information after a valid update request. |
61
|
|
|
* |
62
|
|
|
* @param array $data The data used to update the user. |
63
|
|
|
* @param User $user The user to update. |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public static function update(array $data, User $user): bool |
68
|
|
|
{ |
69
|
|
|
$user->username = $data['username']; |
70
|
|
|
$user->email = $data['email']; |
71
|
|
|
|
72
|
|
|
return $user->save(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|