feat(attachments): refactor attachment management to use attachable pivot table and update related models and tests

This commit is contained in:
2026-06-25 15:30:11 -03:00
parent ee59236d55
commit 7994fe0c96
8 changed files with 184 additions and 40 deletions

View File

@@ -0,0 +1,44 @@
<?php
namespace App\Domains\Attachable\Models;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
#[Fillable([
'attachable_type',
'attachable_id',
'attachment_id',
])]
class AttachableAttachment extends Model
{
protected $table = 'attachable_attachments';
public $timestamps = false;
protected function casts(): array
{
return [
'attachable_id' => 'integer',
'attachment_id' => 'integer',
];
}
/**
* @return MorphTo<Model, $this>
*/
public function attachable(): MorphTo
{
return $this->morphTo();
}
/**
* @return BelongsTo<Attachment, $this>
*/
public function attachment(): BelongsTo
{
return $this->belongsTo(Attachment::class, 'attachment_id');
}
}

View File

@@ -6,12 +6,10 @@ use App\Domains\Attachable\Enums\AttachmentType;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;
#[Fillable([
'attachable_type',
'attachable_id',
'key',
'path',
'filename',
@@ -38,17 +36,16 @@ class Attachment extends Model
protected function casts(): array
{
return [
'attachable_id' => 'integer',
'type' => AttachmentType::class,
'size' => 'integer',
];
}
/**
* @return MorphTo<Model, $this>
* @return HasMany<AttachableAttachment, $this>
*/
public function attachable(): MorphTo
public function attachables(): HasMany
{
return $this->morphTo();
return $this->hasMany(AttachableAttachment::class, 'attachment_id');
}
}

View File

@@ -4,15 +4,21 @@ namespace App\Domains\Attachable\Models\Concerns;
use App\Domains\Attachable\Models\Attachment;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
trait HasAttachments
{
/**
* @return MorphMany<Attachment, Model>
* @return MorphToMany<Attachment, Model, $this>
*/
public function attachments(): MorphMany
public function attachments(): MorphToMany
{
return $this->morphMany(Attachment::class, 'attachable');
return $this->morphToMany(
Attachment::class,
'attachable',
'attachable_attachments',
'attachable_id',
'attachment_id',
);
}
}

View File

@@ -5,7 +5,6 @@ namespace App\Domains\Attachable\Services;
use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Exceptions\AttachmentStorageException;
use App\Domains\Attachable\Models\Attachment;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
@@ -14,13 +13,9 @@ use Throwable;
class AttachmentService
{
public function store(
Model $attachable,
UploadedFile $file,
string $path,
AttachmentType $type,
): Attachment {
$this->ensureAttachableExists($attachable);
$normalizedPath = $this->normalizePath($path);
if ($normalizedPath === '') {
@@ -46,11 +41,11 @@ class AttachmentService
try {
/** @var Attachment $attachment */
$attachment = $attachable->attachments()->create([
$attachment = Attachment::query()->create([
'key' => $key,
'path' => $storedPath,
'filename' => $filename,
'type' => $type,
'type' => $this->resolveAttachmentType($file),
'mime_type' => $file->getClientMimeType() ?? $file->getMimeType() ?? 'application/octet-stream',
'extension' => $file->extension(),
'size' => $file->getSize() ?? 0,
@@ -75,13 +70,6 @@ class AttachmentService
$attachment->delete();
}
protected function ensureAttachableExists(Model $attachable): void
{
if (! $attachable->exists) {
throw new AttachmentStorageException('Cannot manage attachments for an unsaved model.');
}
}
protected function normalizePath(string $path): string
{
return trim($path, '/');
@@ -97,4 +85,39 @@ class AttachmentService
return trim($directory, '/');
}
protected function resolveAttachmentType(UploadedFile $file): AttachmentType
{
$mimeType = strtolower($file->getClientMimeType() ?? $file->getMimeType() ?? '');
if (str_starts_with($mimeType, 'image/')) {
return AttachmentType::Image;
}
if (str_starts_with($mimeType, 'video/')) {
return AttachmentType::Video;
}
if ($mimeType === 'application/pdf') {
return AttachmentType::Pdf;
}
if (str_starts_with($mimeType, 'audio/')) {
return AttachmentType::Audio;
}
if (
str_starts_with($mimeType, 'text/')
|| str_contains($mimeType, 'document')
|| str_contains($mimeType, 'word')
|| str_contains($mimeType, 'excel')
|| str_contains($mimeType, 'spreadsheet')
|| str_contains($mimeType, 'presentation')
|| str_contains($mimeType, 'officedocument')
) {
return AttachmentType::Document;
}
return AttachmentType::Other;
}
}