57 lines
1.7 KiB
PHP
57 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Enums\ServiceCategory;
|
|
use App\Enums\ServiceType;
|
|
use App\Models\Server;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class StoreServiceRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'name' => ['required', 'string', 'max:255'],
|
|
'category' => ['required', Rule::enum(ServiceCategory::class)],
|
|
'type' => ['required', Rule::enum(ServiceType::class)],
|
|
'version' => ['required', 'string', function (string $attribute, mixed $value, \Closure $fail): void {
|
|
if (! isset(config('keystone.services')[$this->category][$this->type]['versions'][$value])) {
|
|
$fail('The selected version is invalid.');
|
|
}
|
|
}],
|
|
];
|
|
}
|
|
|
|
public function after(): array
|
|
{
|
|
return [
|
|
function ($validator): void {
|
|
if ($this->category !== ServiceCategory::GATEWAY->value) {
|
|
return;
|
|
}
|
|
|
|
$server = Server::find($this->route('server'));
|
|
|
|
if ($server?->services()->where('category', ServiceCategory::GATEWAY)->exists()) {
|
|
$validator->errors()->add('category', 'This server already has a gateway service.');
|
|
}
|
|
},
|
|
];
|
|
}
|
|
}
|