3 namespace BookStack\Activity\Notifications;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Activity\Models\Activity;
7 use BookStack\Activity\Models\Loggable;
8 use BookStack\Activity\Notifications\Handlers\CommentCreationNotificationHandler;
9 use BookStack\Activity\Notifications\Handlers\NotificationHandler;
10 use BookStack\Activity\Notifications\Handlers\PageCreationNotificationHandler;
11 use BookStack\Activity\Notifications\Handlers\PageUpdateNotificationHandler;
12 use BookStack\Users\Models\User;
14 class NotificationManager
17 * @var class-string<NotificationHandler>[]
19 protected array $handlers = [];
21 public function handle(Activity $activity, string|Loggable $detail, User $user): void
23 $activityType = $activity->type;
24 $handlersToRun = $this->handlers[$activityType] ?? [];
25 foreach ($handlersToRun as $handlerClass) {
26 /** @var NotificationHandler $handler */
27 $handler = new $handlerClass();
28 $handler->handle($activity, $detail, $user);
33 * @param class-string<NotificationHandler> $handlerClass
35 public function registerHandler(string $activityType, string $handlerClass): void
37 if (!isset($this->handlers[$activityType])) {
38 $this->handlers[$activityType] = [];
41 if (!in_array($handlerClass, $this->handlers[$activityType])) {
42 $this->handlers[$activityType][] = $handlerClass;
46 public function loadDefaultHandlers(): void
48 $this->registerHandler(ActivityType::PAGE_CREATE, PageCreationNotificationHandler::class);
49 $this->registerHandler(ActivityType::PAGE_UPDATE, PageUpdateNotificationHandler::class);
50 $this->registerHandler(ActivityType::COMMENT_CREATE, CommentCreationNotificationHandler::class);