3 namespace BookStack\Activity\Notifications;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Activity\Models\Loggable;
7 use BookStack\Activity\Notifications\Handlers\CommentCreationNotificationHandler;
8 use BookStack\Activity\Notifications\Handlers\NotificationHandler;
9 use BookStack\Activity\Notifications\Handlers\PageCreationNotificationHandler;
10 use BookStack\Activity\Notifications\Handlers\PageUpdateNotificationHandler;
12 class NotificationManager
15 * @var class-string<NotificationHandler>[]
17 protected array $handlers = [];
19 public function handle(string $activityType, string|Loggable $detail): void
21 $handlersToRun = $this->handlers[$activityType] ?? [];
22 foreach ($handlersToRun as $handlerClass) {
23 /** @var NotificationHandler $handler */
24 $handler = app()->make($handlerClass);
25 $handler->handle($activityType, $detail);
30 * @param class-string<NotificationHandler> $handlerClass
32 public function registerHandler(string $activityType, string $handlerClass): void
34 if (!isset($this->handlers[$activityType])) {
35 $this->handlers[$activityType] = [];
38 if (!in_array($handlerClass, $this->handlers[$activityType])) {
39 $this->handlers[$activityType][] = $handlerClass;
43 public function loadDefaultHandlers(): void
45 $this->registerHandler(ActivityType::PAGE_CREATE, PageCreationNotificationHandler::class);
46 $this->registerHandler(ActivityType::PAGE_UPDATE, PageUpdateNotificationHandler::class);
47 $this->registerHandler(ActivityType::COMMENT_CREATE, CommentCreationNotificationHandler::class);