store/app/Services/Account/ProfileService.php

64 lines
2.0 KiB
PHP

<?php
namespace App\Services\Account;
use App\Models\User;
use App\Models\UserProfile;
use App\Services\Concerns\RunsInTransaction;
use App\Services\Concerns\SyncsPhotos;
use App\Services\Media\MediaService;
use Illuminate\Support\Facades\Hash;
class ProfileService
{
use RunsInTransaction, SyncsPhotos;
public function __construct(
private readonly MediaService $mediaService,
) {}
public function update(array $validated, User $user): void
{
$this->runInTransaction(
function () use ($user, $validated): void {
$user->update([
'email' => $validated['email'],
'username' => $validated['username'],
]);
/** @var UserProfile $profile */
$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,
],
);
$this->syncPhotos(
$profile,
[
'photos' => $validated['profile_photo'] ?? null,
'remove_media_ids' => $validated['remove_profile_photo_ids'] ?? null,
's3_keys' => ! empty($validated['profile_s3_key']) ? [$validated['profile_s3_key']] : null,
],
maxPhotos: 1,
required: false,
collection: 'profile_photo',
);
},
'Gagal memperbarui profil',
);
}
public function updatePassword(User $user, string $password): void
{
$user->update([
'password' => Hash::make($password),
]);
}
}