Completed
Push — add-user-settings ( 10fb12 )
by Fèvre
02:25
created

UserRepository::updatePassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace Xetaravel\Models\Repositories;
3
4
use Illuminate\Support\Facades\Hash;
5
use Illuminate\Support\Facades\Request as FacadeRequest;
6
use Xetaravel\Models\User;
7
8
class UserRepository
9
{
10
    /**
11
     * Create a new user instance after a valid registration.
12
     *
13
     * @param array $data The data used to create the user.
14
     *
15
     * @return \Xetaravel\Models\User
16
     */
17
    public static function create(array $data): User
18
    {
19
        $ip = FacadeRequest::ip();
20
21
        return User::create([
22
            'username' => $data['username'],
23
            'email' => $data['email'],
24
            'password' => bcrypt($data['password']),
25
            'register_ip' => $ip,
26
            'last_login_ip' => $ip,
27
            'last_login' => new \DateTime()
28
        ]);
29
    }
30
31
    /**
32
     * Update the user's email after a valid email update.
33
     *
34
     * @param array $data The data used to update the user.
35
     * @param \Xetaravel\Models\User $user The user to update.
36
     *
37
     * @return bool
38
     */
39
    public static function updateEmail(array $data, User $user): bool
40
    {
41
        $user->email = $data['email'];
42
43
        return $user->save();
44
    }
45
46
    /**
47
     * Update the user's password after a valid password update.
48
     *
49
     * @param array $data The data used to update the user.
50
     * @param \Xetaravel\Models\User $user The user to update.
51
     *
52
     * @return bool
53
     */
54
    public static function updatePassword(array $data, User $user): bool
55
    {
56
        $user->password = Hash::make($data['password']);
57
58
        return $user->save();
59
    }
60
61
    /**
62
     * Find the notifications data for the notification sidebar.
63
     *
64
     * @param int $userId The id of the user.
65
     *
66
     * @return array
67
     */
68
    public static function notificationsData($userId): array
69
    {
70
        $user = User::find($userId);
71
72
        return [
73
            'notifications' => $user->notifications->take(8),
74
            'hasUnreadNotifications' => $user->unreadNotifications->isNotEmpty(),
75
            'unredNotificationsCount' => $user->unreadNotifications->count()
76
        ];
77
    }
78
}
79