62 lines
1.8 KiB
PHP
62 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Settings;
|
|
|
|
use App\Actions\Git\GitProviderKey;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class StoreUserGitConnectionRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return $this->user() !== null;
|
|
}
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$this->merge([
|
|
'base_url' => $this->normalizedBaseUrl(),
|
|
'provider' => Str::lower(trim((string) $this->input('provider'))),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'api_token' => ['required', 'string', 'max:2048'],
|
|
'base_url' => [
|
|
'required',
|
|
'url:http,https',
|
|
'max:255',
|
|
Rule::unique('user_git_connections')
|
|
->where(fn ($query) => $query
|
|
->where('user_id', $this->user()->id)
|
|
->where('provider', $this->input('provider'))),
|
|
],
|
|
'provider' => ['required', Rule::in(array_map(
|
|
static fn (GitProviderKey $providerKey): string => $providerKey->value,
|
|
GitProviderKey::cases(),
|
|
))],
|
|
];
|
|
}
|
|
private function normalizedBaseUrl(): string
|
|
{
|
|
$baseUrl = Str::of((string) $this->input('base_url'))
|
|
->trim()
|
|
->rtrim('/')
|
|
->value();
|
|
|
|
return preg_replace('#/api/v1$#i', '', $baseUrl) ?? $baseUrl;
|
|
}
|
|
}
|