Files
WMS/Modules/Helpdesk/Services/AttachmentService.php
T
2026-06-12 09:41:57 +01:00

55 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Modules\Helpdesk\Services;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Modules\Helpdesk\Models\Attachment;
use Modules\Helpdesk\Models\Comment;
use Modules\Helpdesk\Models\Ticket;
final class AttachmentService
{
/**
* Store uploaded files against a ticket and optional comment.
*
* @param Ticket $ticket
* @param array<int, UploadedFile> $files
* @param int $userId
* @param Comment|null $comment
* @return void
*/
public function storeMany(Ticket $ticket, array $files, int $userId, ?Comment $comment = null): void
{
foreach ($files as $file) {
$disk = (string) config('helpdesk.disk');
$path = $file->store('tickets/' . $ticket->id, $disk);
Attachment::query()->create([
'ticket_id' => $ticket->id,
'comment_id' => $comment?->id,
'user_id' => $userId,
'disk' => $disk,
'path' => $path,
'original_name' => $file->getClientOriginalName(),
'mime_type' => $file->getClientMimeType(),
'size' => $file->getSize() ?: 0,
]);
}
}
/**
* Delete a stored attachment and its backing file.
*
* @param Attachment $attachment
* @return void
*/
public function delete(Attachment $attachment): void
{
Storage::disk($attachment->disk)->delete($attachment->path);
$attachment->delete();
}
}