ldc-supportweb/app/Http/Requests/StoreTicket.php

82 lines
2.7 KiB
PHP
Raw Normal View History

2021-04-18 20:31:07 +02:00
<?php
namespace App\Http\Requests;
2021-06-28 17:22:52 +02:00
use App\Enums\Role;
2021-04-18 20:31:07 +02:00
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()
{
2021-07-23 11:04:09 +02:00
/** @var \App\Models\User */
$user = auth()->user();
if (!$user->isAdmin()) {
2021-06-28 17:22:52 +02:00
$this->merge([
'status' => TicketStatus::new()->value,
]);
}
2021-04-18 20:31:07 +02:00
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
2021-07-23 11:34:16 +02:00
'site_id' => [
2021-06-28 17:22:52 +02:00
'required',
2021-07-23 11:34:16 +02:00
'exists:sites,id',
2021-06-28 17:22:52 +02:00
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
2021-07-23 11:04:09 +02:00
/** @var \App\Models\User */
$user = auth()->user();
if ($user->isAdmin()) {
2021-06-29 10:17:43 +02:00
return;
}
2021-07-23 11:34:16 +02:00
if (!in_array($value, $user->sites()->pluck('id')->all())) {
2021-06-28 17:22:52 +02:00
$fail(__('Veuillez choisir entre :values', [
2021-07-23 11:34:16 +02:00
'values' => $user->sites()->pluck('name')->implode(',')
2021-06-28 17:22:52 +02:00
]));
}
}
],
2021-04-18 20:31:07 +02:00
'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',
];
}
}