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;
11 use BookStack\Users\Models\User;
13 class NotificationManager
16 * @var class-string<NotificationHandler>[]
18 protected array $handlers = [];
20 public function handle(string $activityType, string|Loggable $detail, User $user): void
22 $handlersToRun = $this->handlers[$activityType] ?? [];
23 foreach ($handlersToRun as $handlerClass) {
24 /** @var NotificationHandler $handler */
25 $handler = app()->make($handlerClass);
26 $handler->handle($activityType, $detail, $user);
31 * @param class-string<NotificationHandler> $handlerClass
33 public function registerHandler(string $activityType, string $handlerClass): void
35 if (!isset($this->handlers[$activityType])) {
36 $this->handlers[$activityType] = [];
39 if (!in_array($handlerClass, $this->handlers[$activityType])) {
40 $this->handlers[$activityType][] = $handlerClass;
44 public function loadDefaultHandlers(): void
46 $this->registerHandler(ActivityType::PAGE_CREATE, PageCreationNotificationHandler::class);
47 $this->registerHandler(ActivityType::PAGE_UPDATE, PageUpdateNotificationHandler::class);
48 $this->registerHandler(ActivityType::COMMENT_CREATE, CommentCreationNotificationHandler::class);