DiscordDriver::validateSettings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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 DiscordDriver implements ChannelDriverInterface
9
{
10
    public function send(Notification $notification): bool
11
    {
12
        try {
13
            $channel = \Usamamuneerchaudhary\Notifier\Models\NotificationChannel::where('type', 'discord')->first();
14
15
            if (!$channel || !isset($channel->settings['webhook_url'])) {
16
                return false;
17
            }
18
19
            $settings = $channel->settings;
20
            $webhookUrl = $settings['webhook_url'];
21
22
            // Discord webhook payload
23
            $payload = [
24
                'content' => $notification->subject,
25
                'embeds' => [
26
                    [
27
                        'title' => $notification->subject,
28
                        'description' => $notification->content,
29
                        'color' => $settings['color'] ?? 3447003, // Default blue color
30
                        'timestamp' => now()->toIso8601String(),
31
                    ],
32
                ],
33
            ];
34
35
            if (isset($settings['username'])) {
36
                $payload['username'] = $settings['username'];
37
            }
38
39
            if (isset($settings['avatar_url'])) {
40
                $payload['avatar_url'] = $settings['avatar_url'];
41
            }
42
43
            $response = Http::post($webhookUrl, $payload);
44
45
            return $response->successful();
46
        } catch (\Exception $e) {
47
            \Log::error("Discord notification failed: " . $e->getMessage());
48
            return false;
49
        }
50
    }
51
52
    public function validateSettings(array $settings): bool
53
    {
54
        return !empty($settings['webhook_url'] ?? null);
55
    }
56
}
57
58
59
60
61