Completed
Pull Request — master (#10)
by Fèvre
04:32 queued 02:08
created

UserRepository::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
1
<?php
2
namespace Xetaravel\Models\Repositories;
3
4
use Illuminate\Support\Facades\Request as FacadeRequest;
5
use Xetaravel\Models\User;
6
7
class UserRepository
8
{
9
    /**
10
     * Create a new user instance after a valid registration.
11
     *
12
     * @param array $data The data used to create the user.
13
     *
14
     * @return \Xetaravel\Models\User
15
     */
16
    public static function create(array $data): User
17
    {
18
        $ip = FacadeRequest::ip();
19
20
        return User::create([
21
            'username' => $data['username'],
22
            'email' => $data['email'],
23
            'password' => bcrypt($data['password']),
24
            'register_ip' => $ip,
25
            'last_login_ip' => $ip,
26
            'last_login' => new \DateTime()
27
        ]);
28
    }
29
30
    /**
31
     * Find the notifications data for the notification sidebar.
32
     *
33
     * @param int $userId The id of the user.
34
     *
35
     * @return array
36
     */
37
    public static function notificationsData($userId): array
38
    {
39
        $user = User::find($userId);
40
        
41
        return [
42
            'notifications' => $user->notifications->take(8),
43
            'hasUnreadNotifications' => $user->unreadNotifications->isNotEmpty(),
44
            'unredNotificationsCount' => $user->unreadNotifications->count()
45
        ];
46
    }
47
}
48