NotificationRepository   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
eloc 10
c 1
b 0
f 1
dl 0
loc 22
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A findByToken() 0 17 3
1
<?php
2
3
namespace Usamamuneerchaudhary\Notifier\Services;
4
5
use Illuminate\Support\Facades\Cache;
6
use Usamamuneerchaudhary\Notifier\Models\Notification;
7
8
class NotificationRepository
9
{
10
    /**
11
     * Get notification by tracking token from cache/database
12
     */
13
    public function findByToken(string $token): ?Notification
14
    {
15
        $notificationId = Cache::get("notifier:tracking_token:{$token}");
16
17
        if ($notificationId) {
18
            return Notification::find($notificationId);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Usamamuneerchaudh...::find($notificationId) could return the type Illuminate\Database\Eloq...gHasThroughRelationship which is incompatible with the type-hinted return Usamamuneerchaudhary\Not...odels\Notification|null. Consider adding an additional type-check to rule them out.
Loading history...
19
        }
20
21
        $notifications = Notification::whereJsonContains('data->tracking_token', $token)->get();
22
23
        if ($notifications->count() > 0) {
24
            $notification = $notifications->first();
25
            Cache::put("notifier:tracking_token:{$token}", $notification->id, now()->addDays(30));
26
            return $notification;
27
        }
28
29
        return null;
30
    }
31
}
32