]> BookStack Code Mirror - bookstack/blob - app/Activity/Notifications/NotificationManager.php
Notifications: added user preference UI & logic
[bookstack] / app / Activity / Notifications / NotificationManager.php
1 <?php
2
3 namespace BookStack\Activity\Notifications;
4
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
12 class NotificationManager
13 {
14     /**
15      * @var class-string<NotificationHandler>[]
16      */
17     protected array $handlers = [];
18
19     public function handle(string $activityType, string|Loggable $detail): void
20     {
21         $handlersToRun = $this->handlers[$activityType] ?? [];
22         foreach ($handlersToRun as $handlerClass) {
23             /** @var NotificationHandler $handler */
24             $handler = app()->make($handlerClass);
25             $handler->handle($activityType, $detail);
26         }
27     }
28
29     /**
30      * @param class-string<NotificationHandler> $handlerClass
31      */
32     public function registerHandler(string $activityType, string $handlerClass): void
33     {
34         if (!isset($this->handlers[$activityType])) {
35             $this->handlers[$activityType] = [];
36         }
37
38         if (!in_array($handlerClass, $this->handlers[$activityType])) {
39             $this->handlers[$activityType][] = $handlerClass;
40         }
41     }
42
43     public function loadDefaultHandlers(): void
44     {
45         $this->registerHandler(ActivityType::PAGE_CREATE, PageCreationNotificationHandler::class);
46         $this->registerHandler(ActivityType::PAGE_UPDATE, PageUpdateNotificationHandler::class);
47         $this->registerHandler(ActivityType::COMMENT_CREATE, CommentCreationNotificationHandler::class);
48     }
49 }