1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Models\Repositories; |
3
|
|
|
|
4
|
|
|
use Illuminate\Support\Collection; |
5
|
|
|
use Illuminate\Support\Facades\Hash; |
6
|
|
|
use Illuminate\Support\Facades\Request as FacadeRequest; |
7
|
|
|
use Xetaravel\Models\User; |
8
|
|
|
|
9
|
|
|
class UserRepository |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Find the authors of articles with most articles for the sidebar. |
13
|
|
|
* |
14
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
15
|
|
|
*/ |
16
|
|
|
public static function sidebar(): Collection |
17
|
|
|
{ |
18
|
|
|
return User::where('article_count', '>=', 1) |
19
|
|
|
->take(config('xetaravel.blog.users_sidebar')) |
20
|
|
|
->orderBy('article_count', 'desc') |
21
|
|
|
->get(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create a new user instance after a valid registration. |
26
|
|
|
* |
27
|
|
|
* @param array $data The data used to create the user. |
28
|
|
|
* @param array $providerData The additional data provided by the provider. |
29
|
|
|
* @param bool $provider Whether the user is registered with a Social Provider. |
30
|
|
|
* |
31
|
|
|
* @return \Xetaravel\Models\User |
32
|
|
|
*/ |
33
|
|
|
public static function create(array $data, array $providerData = [], bool $provider = false): User |
34
|
|
|
{ |
35
|
|
|
$ip = FacadeRequest::ip(); |
36
|
|
|
|
37
|
|
|
$user = [ |
38
|
|
|
'username' => $data['username'], |
39
|
|
|
'email' => $data['email'], |
40
|
|
|
'register_ip' => $ip, |
41
|
|
|
'last_login_ip' => $ip, |
42
|
|
|
'last_login' => new \DateTime() |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
if ($provider === false) { |
46
|
|
|
$user += [ |
47
|
|
|
'password' => bcrypt($data['password']) |
48
|
|
|
]; |
49
|
|
|
} else { |
50
|
|
|
$user += $providerData; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return User::create($user); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Update the user informations after a valid update request. |
58
|
|
|
* |
59
|
|
|
* @param array $data The data used to update the user. |
60
|
|
|
* @param \Xetaravel\Models\User $user The user to update. |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public static function update(array $data, User $user): bool |
65
|
|
|
{ |
66
|
|
|
$user->username = $data['username']; |
67
|
|
|
$user->email = $data['email']; |
68
|
|
|
|
69
|
|
|
return $user->save(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Update the user's email after a valid email update. |
74
|
|
|
* |
75
|
|
|
* @param array $data The data used to update the user. |
76
|
|
|
* @param \Xetaravel\Models\User $user The user to update. |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public static function updateEmail(array $data, User $user): bool |
81
|
|
|
{ |
82
|
|
|
$user->email = $data['email']; |
83
|
|
|
|
84
|
|
|
return $user->save(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Update the user's password after a valid password update. |
89
|
|
|
* |
90
|
|
|
* @param array $data The data used to update the user. |
91
|
|
|
* @param \Xetaravel\Models\User $user The user to update. |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public static function updatePassword(array $data, User $user): bool |
96
|
|
|
{ |
97
|
|
|
$user->password = Hash::make($data['password']); |
98
|
|
|
|
99
|
|
|
return $user->save(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Create the user's password after a valid password create. (For Discord Users) |
104
|
|
|
* |
105
|
|
|
* @param array $data The data used to update the user. |
106
|
|
|
* @param \Xetaravel\Models\User $user The user to update. |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public static function createPassword(array $data, User $user): bool |
111
|
|
|
{ |
112
|
|
|
$user->password = Hash::make($data['password']); |
113
|
|
|
|
114
|
|
|
return $user->save(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Find the notifications data for the notification sidebar. |
119
|
|
|
* |
120
|
|
|
* @param int $userId The id of the user. |
121
|
|
|
* |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public static function notificationsData($userId): array |
125
|
|
|
{ |
126
|
|
|
$user = User::find($userId); |
127
|
|
|
|
128
|
|
|
return [ |
129
|
|
|
'notifications' => $user->notifications()->take(6)->get(), |
130
|
|
|
'hasUnreadNotifications' => $user->unreadNotifications->isNotEmpty(), |
131
|
|
|
'unreadNotificationsCount' => $user->unreadNotifications->count() |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|