1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- <?php
- namespace App\Services\File;
- use App\Models\Enums\FileObjectType;
- use App\Models\Enums\FileSource;
- use App\Models\File;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Auth;
- class FileAssociationService
- {
- /**
- * @param array $fileIds
- * @param string $objectId
- * @param FileObjectType $fileObjectType
- * @return void
- */
- public function association(array $fileIds, string $objectId, FileObjectType $fileObjectType)
- {
- if (! $fileIds) {
- return;
- }
- $files = File::query()
- ->whereIn("id", $fileIds)
- ->where("created_by", Auth::id())
- ->whereNull("object_id")
- ->where("object_type", $fileObjectType->value)
- ->where("created_at", ">=", Carbon::now()->subMinutes(10))
- ->orderBy("created_at")
- ->get();
- foreach ($files as $file) {
- if ($file->source == FileSource::ATTACHMENT->value) {
- $version = File::query()
- ->where("created_at", ">=", Carbon::now()->subMinutes(10))
- ->where("created_by", Auth::id())
- ->where("object_id", $objectId)
- ->where("object_type", $fileObjectType->value)
- ->where("title", $file->title)
- ->count();
- $file->version = $version + 1;
- }
- $file->object_id = $objectId;
- $file->save();
- }
- }
- }
|