PushDriver   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 28
c 1
b 0
f 1
dl 0
loc 48
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateSettings() 0 4 2
B send() 0 39 6
1
<?php
2
3
namespace Usamamuneerchaudhary\Notifier\Services\ChannelDrivers;
4
5
use Illuminate\Support\Facades\Http;
6
use Usamamuneerchaudhary\Notifier\Models\Notification;
7
8
class PushDriver implements ChannelDriverInterface
9
{
10
    public function send(Notification $notification): bool
11
    {
12
        try {
13
            $channel = \Usamamuneerchaudhary\Notifier\Models\NotificationChannel::where('type', 'push')->first();
14
15
            if (!$channel || !isset($channel->settings['firebase_server_key'])) {
16
                return false;
17
            }
18
19
            $user = $notification->user;
0 ignored issues
show
Bug introduced by
The property user does not seem to exist on Usamamuneerchaudhary\Notifier\Models\Notification. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
20
            if (!$user) {
21
                return false;
22
            }
23
24
            $fcmToken = $user->fcm_token ?? $user->push_token ?? null;
25
26
            if (!$fcmToken) {
27
                \Log::warning("Push notification failed: No FCM token for user {$user->id}");
28
                return false;
29
            }
30
31
            $settings = $channel->settings;
32
33
            $response = Http::withHeaders([
34
                'Authorization' => 'key=' . $settings['firebase_server_key'],
35
                'Content-Type' => 'application/json',
36
            ])->post('https://fcm.googleapis.com/fcm/send', [
37
                'to' => $fcmToken,
38
                'notification' => [
39
                    'title' => $notification->subject,
40
                    'body' => $notification->content,
41
                ],
42
                'data' => $notification->data ?? [],
43
            ]);
44
45
            return $response->successful();
46
        } catch (\Exception $e) {
47
            \Log::error("Push notification failed: " . $e->getMessage());
48
            return false;
49
        }
50
    }
51
52
    public function validateSettings(array $settings): bool
53
    {
54
        return !empty($settings['firebase_server_key'] ?? null) &&
55
               !empty($settings['firebase_project_id'] ?? null);
56
    }
57
}
58
59
60
61
62