PolarNotificationRepository   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 56
c 0
b 0
f 0
wmc 4
lcom 0
cbo 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A recent() 0 21 2
A markAsRead() 0 6 1
A create() 0 15 1
1
<?php
2
3
namespace PolarAdmin\Polar\Repositories;
4
5
use Illuminate\Support\Collection;
6
use PolarAdmin\Polar\Models\PolarNotification;
7
use PolarAdmin\Polar\Events\PolarNotificationCreated;
8
use PolarAdmin\Polar\Contracts\Repositories\PolarNotificationRepository as PolarNotificationRepositoryContract;
9
10
class PolarNotificationRepository implements PolarNotificationRepositoryContract
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public function recent($user) : Collection
16
    {
17
        $notifications = PolarNotification::where('user_id', $user->id)
18
            ->where('read', 0)
19
            ->orderBy('created_at', 'desc')
20
            ->limit(8)
21
            ->get();
22
23
        if ($notifications->count() < 8) {
24
            $readNotifications = PolarNotification::where('user_id', $user->id)
25
                ->where('read', 1)
26
                ->orderBy('created_at', 'desc')
27
                ->limit(8 - $notifications->count())
28
                ->get();
29
30
            $notifications = $notifications->merge($readNotifications)
31
                ->sortByDesc('created_at');
32
        }
33
34
        return $notifications->values();
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function markAsRead($user, int $notification_id) : bool
41
    {
42
        return PolarNotification::where('user_id', $user->id)
43
            ->where('id', '<=', $notification_id)
44
            ->update(['read' => 1]);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function create(array $data) : PolarNotification
51
    {
52
        $notification = PolarNotification::create([
53
            'user_id' => $data['user_id'],
54
            'body' => $data['body'],
55
            'status' => $data['status'],
56
            'from' => array_get($data, 'from'),
57
            'url' => array_get($data, 'url'),
58
            'extra' => array_get($data, 'extra'),
59
        ]);
60
61
        event(new PolarNotificationCreated($notification));
62
63
        return $notification;
64
    }
65
}
66