SlackDriver::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 SlackDriver implements ChannelDriverInterface
9
{
10
    public function send(Notification $notification): bool
11
    {
12
        try {
13
            $channel = \Usamamuneerchaudhary\Notifier\Models\NotificationChannel::where('type', 'slack')->first();
14
            
15
            if (!$channel || !isset($channel->settings['webhook_url'])) {
16
                return false;
17
            }
18
19
            $response = Http::post($channel->settings['webhook_url'], [
20
                'text' => $notification->subject,
21
                'blocks' => [
22
                    [
23
                        'type' => 'section',
24
                        'text' => [
25
                            'type' => 'mrkdwn',
26
                            'text' => $notification->content,
27
                        ],
28
                    ],
29
                ],
30
            ]);
31
32
            return $response->successful();
33
        } catch (\Exception $e) {
34
            \Log::error("Slack notification failed: " . $e->getMessage());
35
            return false;
36
        }
37
    }
38
39
    public function validateSettings(array $settings): bool
40
    {
41
        return !empty($settings['webhook_url'] ?? null);
42
    }
43
}