SmsDriver::sendViaTwilio()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 6
c 1
b 0
f 1
dl 0
loc 10
rs 10
cc 1
nc 1
nop 3
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 SmsDriver implements ChannelDriverInterface
9
{
10
    public function send(Notification $notification): bool
11
    {
12
        try {
13
            $channel = \Usamamuneerchaudhary\Notifier\Models\NotificationChannel::where('type', 'sms')->first();
14
15
            if (!$channel) {
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 || !$user->phone) {
21
                return false;
22
            }
23
24
            $settings = $channel->settings;
25
26
            if (isset($settings['twilio_account_sid']) && isset($settings['twilio_auth_token'])) {
27
                return $this->sendViaTwilio($notification, $settings, $user->phone);
28
            }
29
30
            return $this->sendViaGenericApi($notification, $settings, $user->phone);
31
        } catch (\Exception $e) {
32
            \Log::error("SMS notification failed: " . $e->getMessage());
33
            return false;
34
        }
35
    }
36
37
    protected function sendViaTwilio(Notification $notification, array $settings, string $phone): bool
38
    {
39
        $response = Http::withBasicAuth($settings['twilio_account_sid'], $settings['twilio_auth_token'])
40
            ->post("https://api.twilio.com/2010-04-01/Accounts/{$settings['twilio_account_sid']}/Messages.json", [
41
                'To' => $phone,
42
                'From' => $settings['twilio_phone_number'],
43
                'Body' => $notification->content,
44
            ]);
45
46
        return $response->successful();
47
    }
48
49
    protected function sendViaGenericApi(Notification $notification, array $settings, string $phone): bool
50
    {
51
        if (!isset($settings['api_url']) || !isset($settings['api_key'])) {
52
            return false;
53
        }
54
55
        $response = Http::withHeaders([
56
            'Authorization' => 'Bearer ' . $settings['api_key'],
57
            'Content-Type' => 'application/json',
58
        ])->post($settings['api_url'], [
59
            'to' => $phone,
60
            'message' => $notification->content,
61
        ]);
62
63
        return $response->successful();
64
    }
65
66
    public function validateSettings(array $settings): bool
67
    {
68
        // Check for either Twilio settings or generic API settings
69
        $hasTwilio = !empty($settings['twilio_account_sid'] ?? null) &&
70
                    !empty($settings['twilio_auth_token'] ?? null) &&
71
                    !empty($settings['twilio_phone_number'] ?? null);
72
73
        $hasGenericApi = !empty($settings['api_url'] ?? null) &&
74
                        !empty($settings['api_key'] ?? null);
75
76
        return $hasTwilio || $hasGenericApi;
77
    }
78
}
79