NotificationRepository::findByToken()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
c 1
b 0
f 1
dl 0
loc 17
rs 9.9666
cc 3
nc 3
nop 1
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