refactor: with pint

This commit is contained in:
b1ek 2024-08-30 17:40:57 +10:00
parent 9922ebbc39
commit c67c96cf92
Signed by: blek
GPG Key ID: 14546221E3595D0C
23 changed files with 67 additions and 58 deletions

View File

@ -5,6 +5,8 @@ namespace App\Facade\Filters;
class Filter
{
public FilterTypeEnum $type;
public string $column;
public mixed $filter;
}

View File

@ -2,11 +2,9 @@
namespace App\Facade\Filters;
use App\Facade\Filters\Pagination;
use Error;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Exceptions\HttpResponseException;
use Error;
use JsonMapper;
class Filters
@ -25,7 +23,7 @@ class Filters
public static function fromArrayOrObject(array|object $data): Filters
{
$mapper = new JsonMapper();
$mapper = new JsonMapper;
if (is_array($data)) {
$data = json_decode(json_encode($data), false);
}

View File

@ -5,5 +5,6 @@ namespace App\Facade\Filters;
class Order
{
public string $by;
public OrderTypeEnum $sort;
}

View File

@ -5,5 +5,6 @@ namespace App\Facade\Filters;
class Pagination
{
public int $size;
public int $page;
}

View File

@ -11,8 +11,7 @@ class PrivateUserController extends Controller
{
public function __construct(
private UserService $userService
) {
}
) {}
public function list(AuthorizedRequest $request)
{
@ -22,6 +21,7 @@ class PrivateUserController extends Controller
public function listFilters(AuthorizedRequest $request)
{
$filters = Filters::fromArrayOrObject($request->all());
return $this->userService->listAll($filters);
}

View File

@ -10,8 +10,7 @@ class PublicUserController extends Controller
{
public function __construct(
private UserService $userService
) {
}
) {}
public function register(RegisterRequest $request)
{

View File

@ -2,7 +2,6 @@
namespace App\Http\Controllers;
use App\Http\Requests\AuthorizedRequest;
use App\Http\Requests\TrashGroupRequest;
use App\Services\UserTrashService;
@ -10,9 +9,7 @@ class TrashUserController extends Controller
{
public function __construct(
private UserTrashService $userTrashService
) {
}
) {}
public function addMultiple(TrashGroupRequest $request)
{

View File

@ -3,7 +3,6 @@
namespace App\Http\Requests;
use App\Rules\ZxcvbnRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Password;
class LoginRequest extends RestRequest
@ -25,7 +24,7 @@ class LoginRequest extends RestRequest
{
return [
'email' => ['required', 'email'],
'password' => ['required', Password::min(1)->rules([ new ZxcvbnRule() ])],
'password' => ['required', Password::min(1)->rules([new ZxcvbnRule])],
];
}
}

View File

@ -4,7 +4,6 @@ namespace App\Http\Requests;
use App\Models\User;
use App\Rules\ZxcvbnRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Password;
class RegisterRequest extends RestRequest
@ -30,7 +29,7 @@ class RegisterRequest extends RestRequest
'middle_name' => ['required', 'string'],
'email' => ['required', 'email'],
'phone' => ['required', 'regex:'.User::PHONE_REGEX],
'password' => ['required', Password::min(1)->rules([ new ZxcvbnRule() ])],
'password' => ['required', Password::min(1)->rules([new ZxcvbnRule])],
];
}
}

View File

@ -10,14 +10,15 @@ class TrashGroupRequest extends AuthorizedRequest
{
$ids = explode(',', $this->query('ids'));
$validator = validator([
'ids' => $ids
'ids' => $ids,
], [
'ids' => 'required|array|min:1',
'ids.*' => 'required|uuid|distinct'
'ids.*' => 'required|uuid|distinct',
]);
if ($validator->fails()) {
throw new HttpResponseException(response()->json($validator->errors(), 422));
}
return $ids;
}
}

View File

@ -4,7 +4,6 @@ namespace App\Http\Requests;
use App\Models\User;
use App\Rules\ZxcvbnRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Password;
class UserEditRequest extends AuthorizedRequest
@ -23,7 +22,7 @@ class UserEditRequest extends AuthorizedRequest
'user.email' => ['email'],
'user.phone' => ['string', 'regex:'.User::PHONE_REGEX],
'user' => 'required',
'password' => [ Password::min(1)->rules([ new ZxcvbnRule() ]) ],
'password' => [Password::min(1)->rules([new ZxcvbnRule])],
'user.email_verified_at' => 'prohibited',
'user.created_at' => 'prohibited',

View File

@ -12,9 +12,9 @@ use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory;
use HasHistory;
use Notifiable;
use UuidId;
use HasHistory;
public const PHONE_REGEX = '/^\+\d+$/';

View File

@ -16,7 +16,7 @@ class ZxcvbnRule implements ValidationRule
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$value = (string) $value;
$zxcvbn = new Zxcvbn();
$zxcvbn = new Zxcvbn;
if ($zxcvbn->passwordStrength($value)['score'] < 3) {
$fail('Password is not secure enough!');
}

View File

@ -12,8 +12,8 @@ class UserService
{
/**
* Create(register) new user
*
* @param array $data This is expected to be validated already
* @return User
*/
public function create($data, $autoLogin = true): User
{
@ -27,11 +27,13 @@ class UserService
if ($autoLogin) {
Auth::login($user);
}
return $user;
}
/**
* Login to user
*
* @param array $data This is expected to be validated already
* @return User
*/
@ -39,8 +41,10 @@ class UserService
{
if (Auth::attempt($data)) {
request()->session()->regenerate();
return true;
}
return false;
}
@ -62,10 +66,11 @@ class UserService
}
$builder = User::query();
$filters->apply($builder);
return $builder->get()->toArray();
}
public function getOneById(string $id): User | null
public function getOneById(string $id): ?User
{
return User::where(['id' => $id])->first();
}
@ -73,7 +78,7 @@ class UserService
/**
* Will return `null` if failed
*/
public function editUser(array $data, string $id): User | null
public function editUser(array $data, string $id): ?User
{
$user = $this->getOneById($id);
if ($user === null) {
@ -86,10 +91,11 @@ class UserService
$user->fill($data['user']);
$user->save();
return $user;
}
public function getUnexistingIds(array $ids): null | array
public function getUnexistingIds(array $ids): ?array
{
$users = User::whereIn('id', $ids)->get();
if ($users->count() != count($ids)) {
@ -98,8 +104,10 @@ class UserService
return $model->id;
}
)->toArray();
return array_diff($ids, $existingIds);
}
return null;
}
}

View File

@ -8,9 +8,7 @@ class UserTrashService
{
public function __construct(
private UserService $userService
) {
}
) {}
public function moveUsersToTrash(array $ids): bool|array
{

View File

@ -13,10 +13,12 @@ trait UuidId
$model->id = Uuid::uuid6()->toString();
});
}
public function getIncrementing(): bool
{
return false;
}
public function getKeyType(): string
{
return 'string';

View File

@ -30,7 +30,7 @@ class UserFactory extends Factory
'phone' => fake()->unique()->phoneNumber,
'email' => fake()->unique()->safeEmail,
'password' => Hash::make('password'),
'remember_token' => Str::random()
'remember_token' => Str::random(),
];
}

View File

@ -5,7 +5,8 @@ use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
use Illuminate\Support\Facades\Schema;
return new class () extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*/

View File

@ -4,7 +4,8 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class () extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*/

View File

@ -4,7 +4,8 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class () extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*/

View File

@ -4,7 +4,8 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class () extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*/

View File

@ -4,7 +4,8 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class () extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*/