Completed
Pull Request — master (#15)
by
unknown
18:10
created

EloquentNotificationRepository::latestForAdmin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
    public function latestForAdmin(){
20
        return $this->model->whereIsRead(false)->orderBy('created_at', 'desc')->take(10)->get();        
21
    }
22
23
    /**
24
     * Mark the given notification id as "read"
25
     * @param int $notificationId
26
     * @return bool
27
     */
28
    public function markNotificationAsRead($notificationId)
29
    {
30
        $notification = $this->find($notificationId);
31
        $notification->is_read = true;
32
33
        return $notification->save();
34
    }
35
36
    public function all()
37
    {
38
        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 38 which is incompatible with the return type declared by the interface Modules\Core\Repositories\BaseRepository::all of type Illuminate\Database\Eloquent\Collection.
Loading history...
39
    }
40
41
    public function find($id)
42
    {
43
        return $this->model->find($id);
44
    }
45
46
    /**
47
     * Get all the notifications for the given user id
48
     * @param int $userId
49
     * @return \Illuminate\Database\Eloquent\Collection
50
     */
51
    public function allForUser($userId)
52
    {
53
        return $this->model->whereUserId($userId)->orderBy('created_at', 'desc')->get();
54
    }
55
56
    /**
57
     * Get all the read notifications for the given user id
58
     * @param int $userId
59
     * @return \Illuminate\Database\Eloquent\Collection
60
     */
61
    public function allReadForUser($userId)
62
    {
63
        return $this->model->whereUserId($userId)->whereIsRead(true)->orderBy('created_at', 'desc')->get();
64
    }
65
66
    /**
67
     * Get all the unread notifications for the given user id
68
     * @param int $userId
69
     * @return \Illuminate\Database\Eloquent\Collection
70
     */
71
    public function allUnreadForUser($userId)
72
    {
73
        return $this->model->whereUserId($userId)->whereIsRead(false)->orderBy('created_at', 'desc')->get();
74
    }
75
76
    /**
77
     * Delete all the notifications for the given user
78
     * @param int $userId
79
     * @return bool
80
     */
81
    public function deleteAllForUser($userId)
82
    {
83
        return $this->model->whereUserId($userId)->delete();
84
    }
85
86
    /**
87
     * Mark all the notifications for the given user as read
88
     * @param int $userId
89
     * @return bool
90
     */
91
    public function markAllAsReadForUser($userId)
92
    {
93
        return $this->model->whereUserId($userId)->update(['is_read' => true]);
94
    }
95
}
96