56 lines
1.8 KiB
PHP
56 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Notifications\Domain\ValueObjects;
|
|
|
|
use App\Modules\Notifications\Domain\Enums\NotificationType;
|
|
use DateTimeImmutable;
|
|
|
|
/**
|
|
* A notification to add for a user, once per dedupe key.
|
|
*/
|
|
final readonly class NewNotification
|
|
{
|
|
/**
|
|
* @param array<string, int|string> $params
|
|
*/
|
|
public function __construct(
|
|
public int $userId,
|
|
public NotificationType $type,
|
|
public array $params,
|
|
public string $dedupeKey,
|
|
public DateTimeImmutable $createdAt,
|
|
) {}
|
|
|
|
/**
|
|
* @param array<string, int|string> $params
|
|
*/
|
|
public static function taskStartsSoon(int $userId, int $taskId, string $startsOn, array $params, DateTimeImmutable $at): self
|
|
{
|
|
return new self($userId, NotificationType::TaskStartsSoon, $params, "task_starts:{$taskId}:{$startsOn}:{$userId}", $at);
|
|
}
|
|
|
|
/**
|
|
* One notification per maintenance need: the item, its last maintenance
|
|
* and the level (coming up or late).
|
|
*
|
|
* @param array<string, int|string> $params
|
|
*/
|
|
public static function maintenanceDue(int $userId, int $itemId, ?int $lastRecordId, string $level, array $params, DateTimeImmutable $at): self
|
|
{
|
|
return new self($userId, NotificationType::MaintenanceDue, $params, 'maintenance:'.$itemId.':'.($lastRecordId ?? 'start').':'.$level.':'.$userId, $at);
|
|
}
|
|
|
|
/**
|
|
* Each assignment is its own notification: one removed and added again
|
|
* is told again.
|
|
*
|
|
* @param array<string, int|string> $params
|
|
*/
|
|
public static function assigned(NotificationType $type, int $userId, int $subjectId, array $params, DateTimeImmutable $at): self
|
|
{
|
|
return new self($userId, $type, $params, "{$type->value}:{$subjectId}:{$userId}:".$at->format('U.u').':'.bin2hex(random_bytes(4)), $at);
|
|
}
|
|
}
|