2024-08-30 01:28:17 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2024-08-30 09:14:48 +02:00
|
|
|
use App\Facade\Filters\Filters;
|
2024-08-30 01:28:17 +02:00
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
|
|
|
|
class UserService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Create(register) new user
|
2024-08-30 09:40:57 +02:00
|
|
|
*
|
|
|
|
* @param array $data This is expected to be validated already
|
2024-08-30 01:28:17 +02:00
|
|
|
*/
|
|
|
|
public function create($data, $autoLogin = true): User
|
|
|
|
{
|
2024-08-30 09:40:57 +02:00
|
|
|
if (User::where(['email' => $data['email']])->count() !== 0) {
|
2024-08-30 01:28:17 +02:00
|
|
|
throw new HttpResponseException(response()->json('email_taken', 400));
|
|
|
|
}
|
|
|
|
|
|
|
|
$data['password'] = Hash::make($data['password']);
|
|
|
|
|
|
|
|
$user = User::create($data);
|
|
|
|
if ($autoLogin) {
|
|
|
|
Auth::login($user);
|
|
|
|
}
|
2024-08-30 09:40:57 +02:00
|
|
|
|
2024-08-30 01:28:17 +02:00
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Login to user
|
2024-08-30 09:40:57 +02:00
|
|
|
*
|
|
|
|
* @param array $data This is expected to be validated already
|
2024-08-30 01:28:17 +02:00
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
public function login($data): bool
|
|
|
|
{
|
|
|
|
if (Auth::attempt($data)) {
|
|
|
|
request()->session()->regenerate();
|
2024-08-30 09:40:57 +02:00
|
|
|
|
2024-08-30 01:28:17 +02:00
|
|
|
return true;
|
|
|
|
}
|
2024-08-30 09:40:57 +02:00
|
|
|
|
2024-08-30 01:28:17 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-08-30 03:31:22 +02:00
|
|
|
public function setPassword($data)
|
2024-08-30 01:28:17 +02:00
|
|
|
{
|
2024-08-30 09:40:57 +02:00
|
|
|
$user = User::where(['email' => $data['email']])->first();
|
2024-08-30 01:28:17 +02:00
|
|
|
if ($user === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user->password = Hash::make($data['password']);
|
|
|
|
$user->save();
|
|
|
|
}
|
2024-08-30 02:17:13 +02:00
|
|
|
|
2024-08-30 09:14:48 +02:00
|
|
|
public function listAll(?Filters $filters = null): array
|
2024-08-30 02:17:13 +02:00
|
|
|
{
|
2024-08-30 09:14:48 +02:00
|
|
|
if (is_null($filters)) {
|
|
|
|
return User::all()->toArray();
|
|
|
|
}
|
|
|
|
$builder = User::query();
|
|
|
|
$filters->apply($builder);
|
2024-08-30 09:40:57 +02:00
|
|
|
|
2024-08-30 09:14:48 +02:00
|
|
|
return $builder->get()->toArray();
|
2024-08-30 02:17:13 +02:00
|
|
|
}
|
|
|
|
|
2024-08-30 09:40:57 +02:00
|
|
|
public function getOneById(string $id): ?User
|
2024-08-30 02:17:13 +02:00
|
|
|
{
|
2024-08-30 09:40:57 +02:00
|
|
|
return User::where(['id' => $id])->first();
|
2024-08-30 02:17:13 +02:00
|
|
|
}
|
2024-08-30 03:31:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Will return `null` if failed
|
|
|
|
*/
|
2024-08-30 09:40:57 +02:00
|
|
|
public function editUser(array $data, string $id): ?User
|
2024-08-30 03:31:22 +02:00
|
|
|
{
|
|
|
|
$user = $this->getOneById($id);
|
|
|
|
if ($user === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('password', $data)) {
|
2024-08-30 09:40:57 +02:00
|
|
|
$this->setPassword(['email' => $user['email'], 'password' => $data['password']]);
|
2024-08-30 09:49:54 +02:00
|
|
|
unset($data['password']);
|
2024-08-30 03:31:22 +02:00
|
|
|
}
|
|
|
|
|
2024-08-30 09:49:54 +02:00
|
|
|
$user->fill($data);
|
2024-08-30 03:31:22 +02:00
|
|
|
$user->save();
|
2024-08-30 09:40:57 +02:00
|
|
|
|
2024-08-30 03:31:22 +02:00
|
|
|
return $user;
|
|
|
|
}
|
2024-08-30 06:16:16 +02:00
|
|
|
|
2024-08-30 09:40:57 +02:00
|
|
|
public function getUnexistingIds(array $ids): ?array
|
2024-08-30 06:16:16 +02:00
|
|
|
{
|
|
|
|
$users = User::whereIn('id', $ids)->get();
|
|
|
|
if ($users->count() != count($ids)) {
|
|
|
|
$existingIds = $users->map(
|
2024-08-30 09:37:24 +02:00
|
|
|
function ($model) {
|
2024-08-30 06:16:16 +02:00
|
|
|
return $model->id;
|
|
|
|
}
|
|
|
|
)->toArray();
|
2024-08-30 09:40:57 +02:00
|
|
|
|
2024-08-30 06:16:16 +02:00
|
|
|
return array_diff($ids, $existingIds);
|
|
|
|
}
|
2024-08-30 09:40:57 +02:00
|
|
|
|
2024-08-30 06:16:16 +02:00
|
|
|
return null;
|
|
|
|
}
|
2024-08-30 09:37:24 +02:00
|
|
|
}
|