dev-think-one /
laravel-notification-tracker
| 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
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
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
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 |