2017-08-04 19:11:41 -05:00
|
|
|
<?php
|
|
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
namespace App\Services\Users;
|
2017-08-04 19:11:41 -05:00
|
|
|
|
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;
|
2017-08-04 19:11:41 -05:00
|
|
|
|
2017-08-27 15:10:51 -05:00
|
|
|
class UserUpdateService
|
2017-08-04 19:11:41 -05:00
|
|
|
{
|
2017-12-03 14:00:47 -06:00
|
|
|
use HasUserLevels;
|
|
|
|
|
|
2025-03-03 14:41:19 -05:00
|
|
|
public function __construct(private readonly Hasher $hasher) {}
|
2017-08-04 19:11:41 -05:00
|
|
|
|
|
|
|
|
/**
|
2021-08-15 17:37:12 -07:00
|
|
|
* Update the user model instance and return the updated model.
|
2017-08-04 19:11:41 -05:00
|
|
|
*
|
2025-03-03 14:41:19 -05:00
|
|
|
* @param array<array-key, mixed> $data
|
|
|
|
|
*
|
2025-09-08 13:12:33 -04:00
|
|
|
* @throws Throwable
|
2017-08-04 19:11:41 -05:00
|
|
|
*/
|
2021-08-15 17:37:12 -07:00
|
|
|
public function handle(User $user, array $data): User
|
2017-08-04 19:11:41 -05:00
|
|
|
{
|
2021-01-23 12:33:34 -08:00
|
|
|
if (!empty(array_get($data, 'password'))) {
|
2017-08-04 19:11:41 -05:00
|
|
|
$data['password'] = $this->hasher->make($data['password']);
|
2018-02-07 21:13:40 -06:00
|
|
|
} else {
|
|
|
|
|
unset($data['password']);
|
2017-08-04 19:11:41 -05:00
|
|
|
}
|
|
|
|
|
|
2021-08-15 17:37:12 -07:00
|
|
|
$user->forceFill($data)->saveOrFail();
|
2017-12-03 14:00:47 -06:00
|
|
|
|
2021-08-15 17:37:12 -07:00
|
|
|
return $user->refresh();
|
2017-08-04 19:11:41 -05:00
|
|
|
}
|
|
|
|
|
}
|