where('task_id', $taskId)->find($imageId); return $model === null ? null : $this->toEntity($model); } public function filePathsOfTask(int $taskId): array { return TaskImageModel::query() ->where('task_id', $taskId) ->get(['path', 'thumbnail_path']) ->flatMap(fn (TaskImageModel $model): array => [$model->path, $model->thumbnail_path]) ->values() ->all(); } public function save(TaskImage $image): TaskImage { $model = $image->id() === null ? new TaskImageModel : TaskImageModel::query()->findOrFail($image->id()); $model->fill([ 'task_id' => $image->taskId(), 'user_id' => $image->uploaderId(), 'path' => $image->file()->path, 'thumbnail_path' => $image->file()->thumbnailPath, 'original_name' => $image->originalName(), 'caption' => $image->caption()?->value, 'width' => $image->file()->width, 'height' => $image->file()->height, 'created_at' => $image->uploadedAt(), ])->save(); return $this->toEntity($model->refresh()); } public function delete(TaskImage $image): void { TaskImageModel::query()->whereKey($image->id())->delete(); } private function toEntity(TaskImageModel $model): TaskImage { return TaskImage::reconstitute( $model->id, $model->task_id, $model->user_id, new StoredTaskImage($model->path, $model->thumbnail_path, $model->width, $model->height), $model->original_name, TaskImageCaption::fromNullable($model->caption), DateTimeImmutable::createFromInterface($model->created_at), ); } }