70 lines
2.2 KiB
PHP
70 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Account;
|
|
|
|
use App\Models\User;
|
|
use App\Services\Media\MediaService;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class ProfileService
|
|
{
|
|
public function __construct(
|
|
private readonly MediaService $mediaService,
|
|
) {}
|
|
|
|
public function update(array $validated, User $user): void
|
|
{
|
|
try {
|
|
DB::transaction(function () use ($user, $validated): void {
|
|
$user->update([
|
|
'email' => $validated['email'],
|
|
'username' => $validated['username'],
|
|
]);
|
|
|
|
$profile = $user->profile()->updateOrCreate(
|
|
['user_id' => $user->id],
|
|
[
|
|
'full_name' => $validated['full_name'],
|
|
'phone_number' => $validated['phone_number'] ?? null,
|
|
'gender' => $validated['gender'] ?? null,
|
|
'birth_date' => $validated['birth_date'] ?? null,
|
|
'address' => $validated['address'] ?? null,
|
|
],
|
|
);
|
|
|
|
$s3Keys = ! empty($validated['profile_s3_key']) ? [$validated['profile_s3_key']] : null;
|
|
$removeIds = $validated['remove_profile_photo_ids'] ?? null;
|
|
|
|
$this->mediaService->syncCollection(
|
|
$profile,
|
|
'profile_photo',
|
|
null,
|
|
$removeIds,
|
|
1,
|
|
s3Keys: $s3Keys,
|
|
);
|
|
});
|
|
} catch (ValidationException $e) {
|
|
throw $e;
|
|
} catch (\Throwable $e) {
|
|
Log::error('Gagal memperbarui profil: '.$e->getMessage(), [
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
throw ValidationException::withMessages([
|
|
'system' => 'Terjadi kesalahan pada server. Silakan laporkan masalah ini ke pihak terkait.',
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function updatePassword(User $user, string $password): void
|
|
{
|
|
$user->update([
|
|
'password' => Hash::make($password),
|
|
]);
|
|
}
|
|
}
|