EmailDriver   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateSettings() 0 3 1
B send() 0 45 7
1
<?php
2
3
namespace Usamamuneerchaudhary\Notifier\Services\ChannelDrivers;
4
5
use Illuminate\Support\Facades\Log;
6
use Illuminate\Support\Facades\Mail;
7
use Usamamuneerchaudhary\Notifier\Models\Notification;
8
use Usamamuneerchaudhary\Notifier\Services\AnalyticsService;
9
10
class EmailDriver implements ChannelDriverInterface
11
{
12
    public function send(Notification $notification): bool
13
    {
14
        try {
15
            $channel = \Usamamuneerchaudhary\Notifier\Models\NotificationChannel::where('type', 'email')->first();
16
            $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...
17
18
            if (!$user || !$user->email) {
19
                return false;
20
            }
21
22
            $settings = $channel->settings ?? [];
23
            $fromAddress = $settings['from_address'] ?? config('mail.from.address', '[email protected]');
24
            $fromName = $settings['from_name'] ?? config('mail.from.name', 'Notification');
25
26
            // Inject tracking pixel if analytics is enabled
27
            $content = $notification->content;
28
            $trackingToken = $notification->data['tracking_token'] ?? null;
29
30
            if ($trackingToken) {
31
                $analyticsService = app(AnalyticsService::class);
32
                if ($analyticsService->isOpenTrackingEnabled()) {
33
                    $content .= $analyticsService->generateTrackingPixel($trackingToken);
34
                }
35
            }
36
37
            $isHtml = strip_tags($content) !== $content;
38
39
            if ($isHtml) {
40
                Mail::html($content, function (\Illuminate\Mail\Message $message) use ($notification, $user, $fromAddress, $fromName) {
41
                    $message->to($user->email)
42
                            ->subject($notification->subject)
43
                            ->from($fromAddress, $fromName);
44
                });
45
            } else {
46
                Mail::raw($content, function (\Illuminate\Mail\Message $message) use ($notification, $user, $fromAddress, $fromName) {
47
                    $message->to($user->email)
48
                            ->subject($notification->subject)
49
                            ->from($fromAddress, $fromName);
50
                });
51
            }
52
53
            return true;
54
        } catch (\Exception $e) {
55
            Log::error("Email notification failed: " . $e->getMessage());
56
            return false;
57
        }
58
    }
59
60
    public function validateSettings(array $settings): bool
61
    {
62
        return !empty($settings['from_address'] ?? null);
63
    }
64
}
65