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

UserRepository   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 0
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 13 1
A notificationsData() 0 10 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