new helpdesk module
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user