Skip to content

Commit

Permalink
add root notification channel
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jan 30, 2024
1 parent bc8e01f commit b609583
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Models/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Notification extends DatabaseNotification implements Contract
* @var array
*/
protected $appends = [
'content',
'formatted_created_at',
'is_read',
'subject',
Expand Down Expand Up @@ -60,6 +61,20 @@ public function getMorphClass(): string
return static::getProxiedClass();
}

/**
* Get the content attribute.
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute<string, never>
*/
protected function content(): Attribute
{
return new Attribute(
get: function (): string {
return $this->data['content'] ?? '';
}
);
}

/**
* Get the subject attribute.
*
Expand All @@ -85,7 +100,7 @@ protected function formattedCreatedAt(): Attribute
}

/**
* Get the formatted created at attribute.
* Get the is read at attribute.
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute<bool, never>
*/
Expand Down
18 changes: 18 additions & 0 deletions src/Notifications/RootChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Cone\Root\Notifications;

use Illuminate\Notifications\Notification;

class RootChannel
{
/**
* Send the given notification.
*/
public function send(object $notifiable, Notification $notification): void
{
$data = $notification->toRoot($notifiable)->toArray();

$notifiable->rootNotifications()->create($data);
}
}
58 changes: 58 additions & 0 deletions src/Notifications/RootMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Cone\Root\Notifications;

use Illuminate\Contracts\Support\Arrayable;

class RootMessage implements Arrayable
{
/**
* The message subject.
*/
protected ?string $subject = null;

/**
* The message content.
*/
protected ?string $message = null;

/**
* Create a new message instance.
*/
public function __construct(?string $subject = null, ?string $message = null)
{
$this->subject = $subject;
$this->message = $message;
}

/**
* Set the message subject.
*/
public function subject(string $value): static
{
$this->subject = $value;

return $this;
}

/**
* Set the message message.
*/
public function message(string $value): static
{
$this->message = $value;

return $this;
}

/**
* Get the array form of the message.
*/
public function toArray(): array
{
return [
'subject' => $this->subject,
'message' => $this->message,
];
}
}
31 changes: 31 additions & 0 deletions src/Notifications/RootNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Notifications;

use Cone\Root\Notifications\RootChannel;
use Cone\Root\Notifications\RootMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;

abstract class RootNotification extends Notification implements ShouldQueue
{
use Queueable;

/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return [RootChannel::class];
}

/**
* Get the Root Message representation of the notification.
*
* @return array<string, mixed>
*/
abstract public function toRoot(object $notifiable): RootMessage;
}

0 comments on commit b609583

Please sign in to comment.