koel/app/Services/UserService.php

49 lines
1.1 KiB
PHP
Raw Normal View History

2022-06-10 10:47:46 +00:00
<?php
namespace App\Services;
2023-08-20 22:35:58 +00:00
use App\Exceptions\UserProspectUpdateDeniedException;
2022-06-10 10:47:46 +00:00
use App\Models\User;
use Illuminate\Contracts\Hashing\Hasher;
class UserService
{
public function __construct(private Hasher $hash)
{
}
public function createUser(string $name, string $email, string $plainTextPassword, bool $isAdmin): User
{
return User::query()->create([
2022-06-10 10:47:46 +00:00
'name' => $name,
'email' => $email,
'password' => $this->hash->make($plainTextPassword),
'is_admin' => $isAdmin,
]);
}
public function updateUser(User $user, string $name, string $email, string|null $password, bool $isAdmin): User
{
2023-08-20 22:35:58 +00:00
throw_if($user->is_prospect, new UserProspectUpdateDeniedException());
2022-07-29 06:47:10 +00:00
$data = [
2022-06-10 10:47:46 +00:00
'name' => $name,
'email' => $email,
'is_admin' => $isAdmin,
2022-07-29 06:47:10 +00:00
];
if ($password) {
$data['password'] = $this->hash->make($password);
}
$user->update($data);
2022-06-10 10:47:46 +00:00
return $user;
}
public function deleteUser(User $user): void
{
$user->delete();
}
}