ldc-supportweb/app/Http/Requests/StoreTicket.php
2021-07-23 11:43:47 +02:00

81 lines
2.7 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Enums\Role;
use App\Enums\TicketStatus;
use App\Enums\TicketType;
use Illuminate\Foundation\Http\FormRequest;
class StoreTicket extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
/** @var \App\Models\User */
$user = auth()->user();
if (!$user->isAdmin()) {
$this->merge([
'status' => TicketStatus::new()->value,
]);
}
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'site_id' => [
'required',
'exists:sites,id',
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
/** @var \App\Models\User */
$user = auth()->user();
if ($user->isAdmin()) {
return;
}
if (!in_array($value, $user->sites()->pluck('id')->all())) {
$fail(__('Veuillez choisir entre :values', [
'values' => $user->sites()->pluck('name')->implode(',')
]));
}
}
],
'type' => [
'required',
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
if (!in_array($value, TicketType::toValues())) {
$fail(__('Veuillez choisir entre :values', [
'value' => implode(',', TicketType::toLabels())
]));
}
}
],
'status' => [
'required',
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
if (!in_array($value, TicketStatus::toValues())) {
$fail(__('Veuillez choisir entre :values', [
'value' => implode(',', TicketStatus::toLabels())
]));
}
}
],
'text' => 'required|string|min:40',
'file_ticket' => 'mimes:jpeg,jpg,png,pdf|max:2048',
'file_ticket2' => 'mimes:jpeg,jpg,png,pdf|max:2048',
];
}
}