Files
KanbanGit/app/Http/Requests/Settings/StoreUserGitConnectionRequest.php
T
Eric Peters 1c355c20b4
linter / quality (pull_request) Successful in 1m13s
tests / ci (8.4) (pull_request) Failing after 1m15s
DEleted function in request and changed the way the controller uses it,
2026-04-03 16:50:00 +02:00

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;
}
}