EloquentNotificationRepository::allReadForUser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Modules\Notification\Repositories\Eloquent;
4
5
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
6
use Modules\Notification\Repositories\NotificationRepository;
7
8
final class EloquentNotificationRepository extends EloquentBaseRepository implements NotificationRepository
9
{
10
    /**
11
     * @param int $userId
12
     * @return \Illuminate\Database\Eloquent\Collection
13
     */
14
    public function latestForUser($userId)
15
    {
16
        return $this->model->whereUserId($userId)->whereIsRead(false)->orderBy('created_at', 'desc')->take(10)->get();
17
    }
18
19
    /**
20
     * Mark the given notification id as "read"
21
     * @param int $notificationId
22
     * @return bool
23
     */
24
    public function markNotificationAsRead($notificationId)
25
    {
26
        $notification = $this->find($notificationId);
27
        $notification->is_read = true;
28
29
        return $notification->save();
30
    }
31
32
    public function all()
33
    {
34
        return $this->model->all();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->model->all(); of type Illuminate\Database\Eloq...base\Eloquent\Builder[] adds the type Illuminate\Database\Eloquent\Builder[] to the return on line 34 which is incompatible with the return type declared by the interface Modules\Core\Repositories\BaseRepository::all of type Illuminate\Database\Eloquent\Collection.
Loading history...
35
    }
36
37
    public function find($id)
38
    {
39
        return $this->model->find($id);
40
    }
41
42
    /**
43
     * Get all the notifications for the given user id
44
     * @param int $userId
45
     * @return \Illuminate\Database\Eloquent\Collection
46
     */
47
    public function allForUser($userId)
48
    {
49
        return $this->model->whereUserId($userId)->orderBy('created_at', 'desc')->get();
50
    }
51
52
    /**
53
     * Get all the read notifications for the given user id
54
     * @param int $userId
55
     * @return \Illuminate\Database\Eloquent\Collection
56
     */
57
    public function allReadForUser($userId)
58
    {
59
        return $this->model->whereUserId($userId)->whereIsRead(true)->orderBy('created_at', 'desc')->get();
60
    }
61
62
    /**
63
     * Get all the unread notifications for the given user id
64
     * @param int $userId
65
     * @return \Illuminate\Database\Eloquent\Collection
66
     */
67
    public function allUnreadForUser($userId)
68
    {
69
        return $this->model->whereUserId($userId)->whereIsRead(false)->orderBy('created_at', 'desc')->get();
70
    }
71
72
    /**
73
     * Delete all the notifications for the given user
74
     * @param int $userId
75
     * @return bool
76
     */
77
    public function deleteAllForUser($userId)
78
    {
79
        return $this->model->whereUserId($userId)->delete();
80
    }
81
82
    /**
83
     * Mark all the notifications for the given user as read
84
     * @param int $userId
85
     * @return bool
86
     */
87
    public function markAllAsReadForUser($userId)
88
    {
89
        return $this->model->whereUserId($userId)->update(['is_read' => true]);
90
    }
91
}
92