Files
panel-pelican-dev/app/Services/Users/UserUpdateService.php

41 lines
920 B
PHP
Raw Permalink Normal View History

<?php
2024-03-12 22:39:16 -04:00
namespace App\Services\Users;
use App\Events\User\PasswordChanged;
2024-03-12 22:39:16 -04:00
use App\Models\User;
use App\Traits\Services\HasUserLevels;
2025-09-24 13:34:19 +02:00
use Illuminate\Contracts\Hashing\Hasher;
use Throwable;
class UserUpdateService
{
use HasUserLevels;
public function __construct(private readonly Hasher $hasher) {}
/**
* Update the user model instance and return the updated model.
*
* @param array<array-key, mixed> $data
*
* @throws Throwable
*/
public function handle(User $user, array $data): User
{
2021-01-23 12:33:34 -08:00
if (!empty(array_get($data, 'password'))) {
$data['password'] = $this->hasher->make($data['password']);
2018-02-07 21:13:40 -06:00
} else {
unset($data['password']);
}
$user->forceFill($data)->saveOrFail();
if (isset($data['password'])) {
PasswordChanged::dispatch($user);
}
return $user->refresh();
}
}