2023-07-26 00:06:48 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Activity\Notifications;
|
|
|
|
|
|
|
|
use BookStack\Activity\ActivityType;
|
2023-08-15 21:39:39 +08:00
|
|
|
use BookStack\Activity\Models\Activity;
|
2023-07-26 00:06:48 +08:00
|
|
|
use BookStack\Activity\Models\Loggable;
|
|
|
|
use BookStack\Activity\Notifications\Handlers\CommentCreationNotificationHandler;
|
|
|
|
use BookStack\Activity\Notifications\Handlers\NotificationHandler;
|
|
|
|
use BookStack\Activity\Notifications\Handlers\PageCreationNotificationHandler;
|
|
|
|
use BookStack\Activity\Notifications\Handlers\PageUpdateNotificationHandler;
|
2023-08-04 19:27:29 +08:00
|
|
|
use BookStack\Users\Models\User;
|
2023-07-26 00:06:48 +08:00
|
|
|
|
|
|
|
class NotificationManager
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var class-string<NotificationHandler>[]
|
|
|
|
*/
|
|
|
|
protected array $handlers = [];
|
|
|
|
|
2023-08-15 21:39:39 +08:00
|
|
|
public function handle(Activity $activity, string|Loggable $detail, User $user): void
|
2023-07-26 00:06:48 +08:00
|
|
|
{
|
2023-08-15 21:39:39 +08:00
|
|
|
$activityType = $activity->type;
|
2023-07-26 00:06:48 +08:00
|
|
|
$handlersToRun = $this->handlers[$activityType] ?? [];
|
|
|
|
foreach ($handlersToRun as $handlerClass) {
|
|
|
|
/** @var NotificationHandler $handler */
|
2023-08-18 00:57:31 +08:00
|
|
|
$handler = new $handlerClass();
|
2023-08-15 21:39:39 +08:00
|
|
|
$handler->handle($activity, $detail, $user);
|
2023-07-26 00:06:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param class-string<NotificationHandler> $handlerClass
|
|
|
|
*/
|
|
|
|
public function registerHandler(string $activityType, string $handlerClass): void
|
|
|
|
{
|
|
|
|
if (!isset($this->handlers[$activityType])) {
|
|
|
|
$this->handlers[$activityType] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array($handlerClass, $this->handlers[$activityType])) {
|
|
|
|
$this->handlers[$activityType][] = $handlerClass;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function loadDefaultHandlers(): void
|
|
|
|
{
|
|
|
|
$this->registerHandler(ActivityType::PAGE_CREATE, PageCreationNotificationHandler::class);
|
|
|
|
$this->registerHandler(ActivityType::PAGE_UPDATE, PageUpdateNotificationHandler::class);
|
|
|
|
$this->registerHandler(ActivityType::COMMENT_CREATE, CommentCreationNotificationHandler::class);
|
|
|
|
}
|
|
|
|
}
|