61 lines
2.1 KiB
PHP
61 lines
2.1 KiB
PHP
<?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.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize()
|
|
{
|
|
return Auth::guard('club')->check();
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
'message' => 'required|string|max:255|min:5',
|
|
'to' => [
|
|
'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()
|
|
->whereHas('customer', fn (Builder $q) => $q->where('id', $authCustomer->id))
|
|
->whereHas('blocked', fn (Builder $q) => $q->where('id', $customer->id))
|
|
->exists()
|
|
) {
|
|
$fail(trans('Vous ne pouvez pas envoyer de message à cet utilisateur'));
|
|
}
|
|
if (
|
|
BlockList::query()
|
|
->whereHas('customer', fn (Builder $q) => $q->where('id', $customer->id))
|
|
->whereHas('blocked', fn (Builder $q) => $q->where('id', $authCustomer->id))
|
|
->exists()
|
|
) {
|
|
$fail(trans('Vous avez bloqué cet utilisateur'));
|
|
}
|
|
}
|
|
]
|
|
];
|
|
}
|
|
}
|