Completed
Push — manage-users ( 2c714e...6ca97d )
by Fèvre
02:42
created

UserRepository::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
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 informations after a valid update request.
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 update(array $data, User $user): bool
40
    {
41
        $user->username = $data['username'];
42
        $user->email = $data['email'];
43
44
        return $user->save();
45
    }
46
47
    /**
48
     * Update the user's email after a valid email update.
49
     *
50
     * @param array $data The data used to update the user.
51
     * @param \Xetaravel\Models\User $user The user to update.
52
     *
53
     * @return bool
54
     */
55
    public static function updateEmail(array $data, User $user): bool
56
    {
57
        $user->email = $data['email'];
58
59
        return $user->save();
60
    }
61
62
    /**
63
     * Update the user's password after a valid password update.
64
     *
65
     * @param array $data The data used to update the user.
66
     * @param \Xetaravel\Models\User $user The user to update.
67
     *
68
     * @return bool
69
     */
70
    public static function updatePassword(array $data, User $user): bool
71
    {
72
        $user->password = Hash::make($data['password']);
73
74
        return $user->save();
75
    }
76
77
    /**
78
     * Find the notifications data for the notification sidebar.
79
     *
80
     * @param int $userId The id of the user.
81
     *
82
     * @return array
83
     */
84
    public static function notificationsData($userId): array
85
    {
86
        $user = User::find($userId);
87
88
        return [
89
            'notifications' => $user->notifications->take(8),
90
            'hasUnreadNotifications' => $user->unreadNotifications->isNotEmpty(),
91
            'unredNotificationsCount' => $user->unreadNotifications->count()
92
        ];
93
    }
94
}
95