Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Multiple Notifiables #95

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/ScheduledNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Thomasjohnkane\Snooze\Exception\NotificationCancelledException;
use Thomasjohnkane\Snooze\Exception\SchedulingFailedException;
use Thomasjohnkane\Snooze\Models\ScheduledNotification as ScheduledNotificationModel;
use Illuminate\Database\Eloquent\Collection as ModelCollection;


class ScheduledNotification
{
Expand Down Expand Up @@ -66,6 +68,56 @@ public static function create(
]));
}

/**
* @param \Illuminate\Support\Collection|array|mixed $notifiables
* @param Notification $notification
* @param DateTimeInterface $sendAt
* @param array $meta
* @return void
*
*/
public static function createMany(
$notifiables,
Notification $notification,
DateTimeInterface $sendAt,
array $meta = []
): void {

if ($sendAt <= Carbon::now()->subMinute()) {
throw new SchedulingFailedException(sprintf('`send_at` must not be in the past: %s',
$sendAt->format(DATE_ISO8601)));
}

$notifiables = self::formatNotifiables($notifiables);

$modelClass = self::getScheduledNotificationModelClass();
$serializer = app(Serializer::class);

$scheduledNotifiables = [];

foreach ($notifiables as $notifiable) {

$targetId = $notifiable instanceof Model ? $notifiable->getKey() : null;
$targetType = $notifiable instanceof AnonymousNotifiable ? AnonymousNotifiable::class : get_class($notifiable);

$scheduledNotifiables[] = [
'target_id' => $targetId,
'target_type' => $targetType,
'notification_type' => get_class($notification),
'target' => $serializer->serialize($notifiable),
'notification' => $serializer->serialize($notification),
'send_at' => $sendAt,
'meta' => json_encode($meta),
'created_at' => now(),
'updated_at' => now(),
];

}

$modelClass::insert($scheduledNotifiables);

}

public static function find(int $scheduledNotificationId): ?self
{
$modelClass = self::getScheduledNotificationModelClass();
Expand Down Expand Up @@ -313,4 +365,20 @@ private static function collection(Collection $models): Collection
return new self($model);
});
}

/**
* Format the notifiables into a Collection / array if necessary.
*
* @param mixed $notifiables
* @return \Illuminate\Database\Eloquent\Collection|array
*/
protected static function formatNotifiables($notifiables)
{
if (! $notifiables instanceof Collection && ! is_array($notifiables)) {
return $notifiables instanceof Model
? new ModelCollection([$notifiables]) : [$notifiables];
}

return $notifiables;
}
}