api/app/Notifications/ModerationThresholdReached.php

74 lines
2.1 KiB
PHP

<?php
namespace App\Notifications;
use App\Models\MealPosts;
use App\Models\ModerationCase;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
class ModerationThresholdReached extends Notification
{
use Queueable;
public function __construct(public ModerationCase $moderationCase) {}
public function via(object $notifiable): array
{
return ['database'];
}
public function toArray(object $notifiable): array
{
return $this->payload() + [
'title' => __('api.notifications.moderation_threshold_title'),
'body' => __('api.notifications.moderation_threshold_body', [
'type' => $this->targetLabel(),
'count' => $this->moderationCase->reports_count,
]),
'targetTitle' => $this->targetTitle(),
];
}
private function payload(): array
{
return [
'type' => 'moderation_case_threshold',
'moderationCaseId' => $this->moderationCase->getKey(),
'reportableType' => $this->targetType(),
'reportableId' => $this->moderationCase->caseable_id,
'reportsCount' => $this->moderationCase->reports_count,
];
}
private function targetType(): string
{
return match ($this->moderationCase->caseable_type) {
MealPosts::class => 'meal_post',
User::class => 'user',
default => 'unknown',
};
}
private function targetLabel(): string
{
return match ($this->moderationCase->caseable_type) {
MealPosts::class => __('api.reports.targets.meal_post'),
User::class => __('api.reports.targets.user'),
default => __('api.reports.targets.unknown'),
};
}
private function targetTitle(): string
{
$target = $this->moderationCase->caseable;
return match (true) {
$target instanceof MealPosts => $target->title,
$target instanceof User => $target->name,
default => (string) $this->moderationCase->caseable_id,
};
}
}