Everyone who may change a task's status (attached subcontractors too) comments on it on the job page. Authors edit and remove their own comments; admin and management may remove anyone's (jobs.remove-any-task-comment). Edited comments show when they were edited, and task cards show the number of comments. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
111 lines
2.6 KiB
PHP
111 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Jobs\Domain\Entities;
|
|
|
|
use App\Modules\Jobs\Domain\Exceptions\NotTaskCommentAuthor;
|
|
use App\Modules\Jobs\Domain\ValueObjects\TaskCommentBody;
|
|
use DateTimeImmutable;
|
|
|
|
/**
|
|
* A comment on a task. Its author may edit and remove it; others may only
|
|
* remove it, and only when allowed to remove anyone's comments.
|
|
*/
|
|
final class TaskComment
|
|
{
|
|
private function __construct(
|
|
private ?int $id,
|
|
private int $taskId,
|
|
private ?int $authorId,
|
|
private TaskCommentBody $body,
|
|
private DateTimeImmutable $createdAt,
|
|
private ?DateTimeImmutable $editedAt,
|
|
) {}
|
|
|
|
public static function create(int $taskId, int $authorId, TaskCommentBody $body, DateTimeImmutable $at): self
|
|
{
|
|
return new self(null, $taskId, $authorId, $body, $at, null);
|
|
}
|
|
|
|
/**
|
|
* The author is null when the user no longer exists.
|
|
*/
|
|
public static function reconstitute(int $id, int $taskId, ?int $authorId, TaskCommentBody $body, DateTimeImmutable $createdAt, ?DateTimeImmutable $editedAt): self
|
|
{
|
|
return new self($id, $taskId, $authorId, $body, $createdAt, $editedAt);
|
|
}
|
|
|
|
/**
|
|
* Saving the same text again changes nothing.
|
|
*
|
|
* @throws NotTaskCommentAuthor
|
|
*/
|
|
public function edit(int $userId, TaskCommentBody $body, DateTimeImmutable $at): void
|
|
{
|
|
$this->ensureAuthor($userId);
|
|
|
|
if ($body->value === $this->body->value) {
|
|
return;
|
|
}
|
|
|
|
$this->body = $body;
|
|
$this->editedAt = $at;
|
|
}
|
|
|
|
/**
|
|
* @throws NotTaskCommentAuthor
|
|
*/
|
|
public function ensureRemovableBy(int $userId, bool $mayRemoveOthers): void
|
|
{
|
|
if (! $mayRemoveOthers) {
|
|
$this->ensureAuthor($userId);
|
|
}
|
|
}
|
|
|
|
public function isWrittenBy(int $userId): bool
|
|
{
|
|
return $this->authorId === $userId;
|
|
}
|
|
|
|
public function id(): ?int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function taskId(): int
|
|
{
|
|
return $this->taskId;
|
|
}
|
|
|
|
public function authorId(): ?int
|
|
{
|
|
return $this->authorId;
|
|
}
|
|
|
|
public function body(): TaskCommentBody
|
|
{
|
|
return $this->body;
|
|
}
|
|
|
|
public function createdAt(): DateTimeImmutable
|
|
{
|
|
return $this->createdAt;
|
|
}
|
|
|
|
public function editedAt(): ?DateTimeImmutable
|
|
{
|
|
return $this->editedAt;
|
|
}
|
|
|
|
/**
|
|
* @throws NotTaskCommentAuthor
|
|
*/
|
|
private function ensureAuthor(int $userId): void
|
|
{
|
|
if (! $this->isWrittenBy($userId)) {
|
|
throw new NotTaskCommentAuthor((int) $this->id, $userId);
|
|
}
|
|
}
|
|
}
|