ldc-legaulois/app/Http/Requests/Club/Messages/SendMessageRequest.php

58 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2024-07-12 16:33:26 +02:00
<?php
namespace App\Http\Requests\Club\Messages;
use App\Models\BlockList;
use App\Models\Customer;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
class SendMessageRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
2024-07-12 16:33:26 +02:00
{
return Auth::guard('club')->check();
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
2024-07-12 16:33:26 +02:00
{
return [
2024-07-17 12:45:43 +02:00
'message' => 'required|string|max:255|min:5',
2024-09-24 16:48:16 +02:00
'to' => [
2024-07-12 16:33:26 +02:00
'required',
Rule::exists('customers', 'username'),
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
function (string $attribute, $value, callable $fail) {
/** @var \App\Models\Customer|null */
$authCustomer = Auth::guard('club')->user();
$customer = Customer::query()->where('username', $value)->firstOrFail();
if (
BlockList::query()
2024-10-21 16:57:20 +02:00
->whereHas('customer', fn (Builder $q) => $q->where('id', $customer->getKey()))
->whereHas('blocked', fn (Builder $q) => $q->where('id', $authCustomer->getKey()))
2024-09-24 16:48:16 +02:00
->exists()
2024-07-12 16:33:26 +02:00
) {
$fail(trans('Vous ne pouvez pas envoyer de message à cet utilisateur'));
}
if (
BlockList::query()
2024-10-21 16:57:20 +02:00
->whereHas('customer', fn (Builder $q) => $q->where('id', $authCustomer->getKey()))
->whereHas('blocked', fn (Builder $q) => $q->where('id', $customer->getKey()))
2024-09-24 16:48:16 +02:00
->exists()
2024-07-12 16:33:26 +02:00
) {
$fail(trans('Vous avez bloqué cet utilisateur'));
}
2026-09-25 16:25:31 +02:00
},
],
2024-07-12 16:33:26 +02:00
];
}
}