]> BookStack Code Mirror - bookstack/blob - app/Activity/Notifications/NotificationManager.php
Notifications: Started core user notification 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 use BookStack\Users\Models\User;
12
13 class NotificationManager
14 {
15     /**
16      * @var class-string<NotificationHandler>[]
17      */
18     protected array $handlers = [];
19
20     public function handle(string $activityType, string|Loggable $detail, User $user): void
21     {
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);
27         }
28     }
29
30     /**
31      * @param class-string<NotificationHandler> $handlerClass
32      */
33     public function registerHandler(string $activityType, string $handlerClass): void
34     {
35         if (!isset($this->handlers[$activityType])) {
36             $this->handlers[$activityType] = [];
37         }
38
39         if (!in_array($handlerClass, $this->handlers[$activityType])) {
40             $this->handlers[$activityType][] = $handlerClass;
41         }
42     }
43
44     public function loadDefaultHandlers(): void
45     {
46         $this->registerHandler(ActivityType::PAGE_CREATE, PageCreationNotificationHandler::class);
47         $this->registerHandler(ActivityType::PAGE_UPDATE, PageUpdateNotificationHandler::class);
48         $this->registerHandler(ActivityType::COMMENT_CREATE, CommentCreationNotificationHandler::class);
49     }
50 }