Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| PlatformAccountsController | |
0.00% |
0 / 46 |
|
0.00% |
0 / 7 |
110 | |
0.00% |
0 / 1 |
| index | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| create | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| store | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
12 | |||
| edit | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| update | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| destroy | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| setActive | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers; |
| 4 | |
| 5 | use App\Models\PlatformAccount; |
| 6 | use App\Models\PlatformInstance; |
| 7 | use App\Enums\PlatformEnum; |
| 8 | use Illuminate\Http\Request; |
| 9 | use Illuminate\Contracts\View\View; |
| 10 | use Illuminate\Http\RedirectResponse; |
| 11 | use Illuminate\Support\Facades\View as ViewFacade; |
| 12 | |
| 13 | class PlatformAccountsController extends Controller |
| 14 | { |
| 15 | public function index(): View |
| 16 | { |
| 17 | $accounts = PlatformAccount::orderBy('platform')->orderBy('created_at', 'desc')->get(); |
| 18 | |
| 19 | return view('pages.platforms.index', compact('accounts')); |
| 20 | } |
| 21 | |
| 22 | public function create(): View |
| 23 | { |
| 24 | return ViewFacade::make('pages.platforms.create'); |
| 25 | } |
| 26 | |
| 27 | public function store(Request $request): RedirectResponse |
| 28 | { |
| 29 | $validated = $request->validate([ |
| 30 | 'platform' => 'required|in:lemmy,mastodon,reddit', |
| 31 | 'instance_url' => 'required|url', |
| 32 | 'username' => 'required|string|max:255', |
| 33 | 'password' => 'required|string', |
| 34 | 'settings' => 'nullable|array', |
| 35 | ]); |
| 36 | |
| 37 | // Create or find platform instance |
| 38 | $platformEnum = PlatformEnum::from($validated['platform']); |
| 39 | $instance = PlatformInstance::firstOrCreate([ |
| 40 | 'platform' => $platformEnum, |
| 41 | 'url' => $validated['instance_url'], |
| 42 | ], [ |
| 43 | 'name' => parse_url($validated['instance_url'], PHP_URL_HOST), |
| 44 | 'description' => ucfirst($validated['platform']) . ' instance', |
| 45 | 'is_active' => true, |
| 46 | ]); |
| 47 | |
| 48 | $account = PlatformAccount::create($validated); |
| 49 | |
| 50 | // If this is the first account for this platform, make it active |
| 51 | if (! PlatformAccount::where('platform', $validated['platform'])->where('is_active', true)->exists()) { |
| 52 | $account->setAsActive(); |
| 53 | } |
| 54 | |
| 55 | // Check if there's a redirect_to parameter for onboarding flow |
| 56 | $redirectTo = $request->input('redirect_to'); |
| 57 | if ($redirectTo) { |
| 58 | return redirect($redirectTo) |
| 59 | ->with('success', 'Platform account created successfully!'); |
| 60 | } |
| 61 | |
| 62 | return redirect()->route('platforms.index') |
| 63 | ->with('success', 'Platform account created successfully!'); |
| 64 | } |
| 65 | |
| 66 | public function edit(PlatformAccount $platformAccount): View |
| 67 | { |
| 68 | return ViewFacade::make('pages.platforms.edit', compact('platformAccount')); |
| 69 | } |
| 70 | |
| 71 | public function update(Request $request, PlatformAccount $platformAccount): RedirectResponse |
| 72 | { |
| 73 | $validated = $request->validate([ |
| 74 | 'instance_url' => 'required|url', |
| 75 | 'username' => 'required|string|max:255', |
| 76 | 'password' => 'nullable|string', |
| 77 | 'settings' => 'nullable|array', |
| 78 | ]); |
| 79 | |
| 80 | // Don't update password if not provided |
| 81 | if (empty($validated['password'])) { |
| 82 | unset($validated['password']); |
| 83 | } |
| 84 | |
| 85 | $platformAccount->update($validated); |
| 86 | |
| 87 | return redirect()->route('platforms.index') |
| 88 | ->with('success', 'Platform account updated successfully!'); |
| 89 | } |
| 90 | |
| 91 | public function destroy(PlatformAccount $platformAccount): RedirectResponse |
| 92 | { |
| 93 | $platformAccount->delete(); |
| 94 | |
| 95 | return redirect()->route('platforms.index') |
| 96 | ->with('success', 'Platform account deleted successfully!'); |
| 97 | } |
| 98 | |
| 99 | public function setActive(PlatformAccount $platformAccount): RedirectResponse |
| 100 | { |
| 101 | $platformAccount->setAsActive(); |
| 102 | |
| 103 | return redirect()->route('platforms.index') |
| 104 | ->with('success', "Set $platformAccount->username@$platformAccount->instance_url as active for {$platformAccount->platform->value}!"); |
| 105 | } |
| 106 | } |