| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- namespace App\Services;
- use App\Enums\ApprovalStatusEnum;
- use App\Models\Client;
- use App\Models\Media;
- use App\Models\Provider;
- use App\Traits\RemoveArchiveS3;
- use App\Traits\UploadsFile;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Database\Eloquent\ModelNotFoundException;
- use Illuminate\Http\UploadedFile;
- use Illuminate\Pagination\LengthAwarePaginator;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class MediaService
- {
- use RemoveArchiveS3, UploadsFile;
- public function getAll(): Collection
- {
- return Media::query()
- ->with(['user'])
- ->orderBy('created_at', 'desc')
- ->get();
- }
- public function findById(int $id): ?Media
- {
- return Media::with(['user'])->find($id);
- }
- public function create(array $data): Media
- {
- return Media::create($data);
- }
- public function update(int $id, array $data): ?Media
- {
- $model = $this->findById($id);
- if (! $model) {
- return null;
- }
- $model->update($data);
- return $model->fresh(['user']);
- }
- public function delete(int $id): bool
- {
- $model = $this->findById($id);
- if (! $model) {
- return false;
- }
- return $model->delete();
- }
- //
- public function getPendingProfileMedia(int $page = 1, int $perPage = 10): LengthAwarePaginator
- {
- return Media::query()
- ->where(function ($query) {
- $query->where(function ($query) {
- $query->where('media.source', 'provider')
- ->whereExists(function ($query) {
- $query->selectRaw('1')
- ->from('providers')
- ->whereColumn('providers.id', 'media.source_id')
- ->whereColumn('providers.profile_media_id', 'media.id')
- ->where('providers.approval_status', ApprovalStatusEnum::ACCEPTED->value)
- ->where('providers.selfie_verified', false)
- ->whereNull('providers.deleted_at');
- });
- })->orWhere(function ($query) {
- $query->where('media.source', 'client')
- ->whereExists(function ($query) {
- $query->selectRaw('1')
- ->from('clients')
- ->whereColumn('clients.id', 'media.source_id')
- ->whereColumn('clients.profile_media_id', 'media.id')
- ->where('clients.selfie_verified', false)
- ->whereNull('clients.deleted_at');
- });
- });
- })
- ->orderBy('media.created_at')
- ->paginate($perPage, ['media.*'], 'page', $page);
- }
- public function approveProfileMedia(string $source, int $sourceId): Media
- {
- return DB::transaction(function () use ($source, $sourceId) {
- $profile = match ($source) {
- 'client' => Client::query()->lockForUpdate()->findOrFail($sourceId),
- 'provider' => Provider::query()->lockForUpdate()->findOrFail($sourceId),
- };
- $profile->load('profileMedia');
- $media = $profile->profileMedia;
- if ($media === null) {
- throw (new ModelNotFoundException)->setModel(Media::class);
- }
- $profile->update(['selfie_verified' => true]);
- return $media;
- });
- }
- public function createFromFile(UploadedFile $file, string $folder, string $source, int $sourceId, ?string $filename = null): Media
- {
- Log::warning('[avatar-upload] MediaService::createFromFile — iniciando upload para S3', [
- 'folder' => $folder,
- 'source' => $source,
- 'source_id' => $sourceId,
- 'file_original_name' => $file->getClientOriginalName(),
- 'file_size' => $file->getSize(),
- 'file_mime' => $file->getMimeType(),
- 'file_extension' => $file->getClientOriginalExtension(),
- 'file_is_valid' => $file->isValid(),
- ]);
- $path = $this->uploadFile($file, $folder, $filename);
- Log::warning('[avatar-upload] MediaService::createFromFile — upload concluído, criando registro Media', [
- 'path_retornado' => $path,
- 'source' => $source,
- 'source_id' => $sourceId,
- ]);
- $media = Media::create([
- 'source' => $source,
- 'source_id' => $sourceId,
- 'name' => $file->getClientOriginalName(),
- 'path' => $path,
- ]);
- Log::warning('[avatar-upload] MediaService::createFromFile — Media criado no banco', [
- 'media_id' => $media->id,
- 'media_path' => $media->path,
- 'media_url' => $media->url ?? null,
- ]);
- return $media;
- }
- public function replaceFile(UploadedFile $newFile, string $folder, string $source, int $sourceId, ?Media $old = null, ?string $filename = null): Media
- {
- Log::warning('[avatar-upload] MediaService::replaceFile iniciado', [
- 'folder' => $folder,
- 'source' => $source,
- 'source_id' => $sourceId,
- 'has_old_media' => $old !== null,
- 'old_media_id' => $old?->id,
- 'old_path' => $old?->path,
- ]);
- if ($old) {
- Log::warning('[avatar-upload] Removendo arquivo antigo do S3', ['old_path' => $old->path]);
- $this->removeArchiveByPath($old->path);
- $old->delete();
- Log::warning('[avatar-upload] Arquivo antigo removido do S3 e Media deletado do banco');
- }
- return $this->createFromFile($newFile, $folder, $source, $sourceId, $filename);
- }
- }
|