45 lines
2.0 KiB
PHP
45 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\IntegrationSetting;
|
|
use App\Services\IntegrationSettings;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class IntegrationSettingsController extends Controller
|
|
{
|
|
public function update(Request $request, string $provider, IntegrationSettings $settings): RedirectResponse
|
|
{
|
|
abort_unless($request->user()->role === 'admin', 403);
|
|
abort_unless(in_array($provider, ['parcel2go', 'bambuddy', 'dryer'], true), 404);
|
|
|
|
$rules = match ($provider) {
|
|
'parcel2go' => [
|
|
'client_id' => ['nullable', 'string', 'max:255'],
|
|
'client_secret' => ['nullable', 'string', 'max:255'],
|
|
'auth_url' => ['nullable', 'url:https', 'max:2048'],
|
|
'base_url' => ['nullable', 'url:https', 'max:2048'],
|
|
'scope' => ['nullable', 'string', 'in:public-api,public-api my-profile,public-api payment,public-api payment my-profile'],
|
|
'collection_postcode' => ['nullable', 'string', 'max:12'],
|
|
'profile_client_id' => ['nullable', 'string', 'max:255'],
|
|
],
|
|
default => [
|
|
'url' => ['nullable', 'url:https', 'max:2048'],
|
|
'api_key' => ['nullable', 'string', 'max:512'],
|
|
],
|
|
};
|
|
$submitted = array_filter($request->validate($rules), fn (?string $value): bool => filled($value));
|
|
$effective = $settings->for($provider);
|
|
if ($submitted === [] && ! collect($effective)->contains(fn ($value): bool => filled($value))) {
|
|
return redirect()->route('settings.index')->with('status', 'No integration settings changed.');
|
|
}
|
|
|
|
$record = IntegrationSetting::find($provider) ?? new IntegrationSetting(['provider' => $provider]);
|
|
$record->settings = array_replace($effective, $submitted);
|
|
$record->save();
|
|
|
|
return redirect()->route('settings.index')->with('status', ucfirst($provider).' settings saved.');
|
|
}
|
|
}
|