By dedupe key. */ public array $notifications = []; /** @var array */ public array $seenAt = []; public function addIfNew(NewNotification $notification): bool { if (isset($this->notifications[$notification->dedupeKey])) { return false; } $this->notifications[$notification->dedupeKey] = $notification; return true; } public function markSeen(int $userId, DateTimeImmutable $at): void { foreach ($this->notifications as $key => $notification) { if ($notification->userId === $userId && ! isset($this->seenAt[$key])) { $this->seenAt[$key] = $at; } } } public function deleteOlderThan(DateTimeImmutable $moment): void { $this->notifications = array_filter($this->notifications, fn (NewNotification $notification): bool => $notification->createdAt >= $moment); } /** * Who was told about which task: "userId:taskId", in order of adding. * * @return list */ public function recipients(): array { return array_values(array_map(fn (NewNotification $notification): string => $notification->userId.':'.$notification->params['task_id'], $this->notifications)); } }