Passed
Push — main ( 8b0f26...0acacf )
by Yaroslav
03:02
created

Tracker   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 97.3%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 76
ccs 36
cts 37
cp 0.973
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A notificationMeta() 0 11 3
A trackerMeta() 0 11 3
A trackMailMessage() 0 9 1
A track() 0 18 4
1
<?php
2
3
namespace NotificationTracker\Notification;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Notifications\Messages\MailMessage;
7
use Illuminate\Notifications\Notification;
8
use NotificationTracker\Models\TrackedChannel;
9
use NotificationTracker\Models\TrackedNotification;
10
use NotificationTracker\NotificationTracker;
11
use Symfony\Component\Mime\Email;
12
13
class Tracker
14
{
15
    protected TrackedNotification $trackedNotification;
16
    protected TrackedChannel $trackedChannel;
17
18
    /**
19
     * @param Notification $notification
20
     * @throws \Exception
21
     */
22 5
    public function __construct(protected Notification $notification)
23
    {
24 5
        $this->trackedNotification = NotificationTracker::model('notification', [
25 5
            'class' => substr((($notification instanceof Trackable) ? $notification->getClassAlias() : get_class($notification)), -255),
26 5
            'data'  => serialize($notification),
27 5
        ]);
28
29
        // we need initialise value to allow update meta
30 5
        $this->trackedChannel = NotificationTracker::model('channel', []);
31
    }
32
33 5
    public function track($channel, $notifiable): TrackedChannel
34
    {
35 5
        if (!$this->trackedNotification->exists || $this->trackedNotification->isDirty()) {
36 5
            $this->trackedNotification->save();
37
        }
38
39 5
        if ($notifiable instanceof Model) {
40
            $this->trackedChannel->meta->toMorph('receiver', $notifiable);
41
        }
42
43 5
        $this->trackedChannel->fill([
44 5
            'channel' => (string)$channel,
45 5
            'route'   => serialize($notifiable->routeNotificationFor('mail', $this->notification)),
46 5
        ]);
47
48 5
        $this->trackedNotification->channels()->save($this->trackedChannel);
49
50 5
        return $this->trackedChannel;
51
    }
52
53 5
    public function trackMailMessage(MailMessage $mailMessageRaw, $notifiable, $channel = 'mail'): MailMessage
54
    {
55 5
        $trackedChannel = $this->track($channel, $notifiable);
56
57 5
        return $mailMessageRaw
58 5
            ->withSymfonyMessage(
59 5
                fn (Email $message) => $message
60 5
                    ->getHeaders()
61 5
                    ->addTextHeader(NotificationTracker::trackHeaderName(), $trackedChannel->getTrackerId())
62 5
            );
63
    }
64
65 3
    public function trackerMeta(string|\Closure $key = null, mixed $value = null): static
66
    {
67 3
        if (is_callable($key)) {
68 3
            call_user_func($key, $this->trackedChannel->meta, $this->trackedChannel);
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type null; however, parameter $callback of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
            call_user_func(/** @scrutinizer ignore-type */ $key, $this->trackedChannel->meta, $this->trackedChannel);
Loading history...
69
        }
70
71 3
        if (is_string($key)) {
72 1
            $this->trackedChannel->meta->setAttribute($key, $value);
73
        }
74
75 3
        return $this;
76
    }
77
78 3
    public function notificationMeta(string|\Closure $key = null, mixed $value = null): static
79
    {
80 3
        if (is_callable($key)) {
81 3
            call_user_func($key, $this->trackedNotification->meta, $this->trackedNotification);
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type null; however, parameter $callback of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
            call_user_func(/** @scrutinizer ignore-type */ $key, $this->trackedNotification->meta, $this->trackedNotification);
Loading history...
82
        }
83
84 3
        if (is_string($key)) {
85 1
            $this->trackedNotification->meta->setAttribute($key, $value);
86
        }
87
88 3
        return $this;
89
    }
90
}
91