Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 73
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
PlatformAccountsController
0.00% covered (danger)
0.00%
0 / 73
0.00% covered (danger)
0.00%
0 / 6
210
0.00% covered (danger)
0.00%
0 / 1
 index
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 store
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
20
 show
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 update
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
 destroy
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 setActive
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace App\Http\Controllers\Api\V1;
4
5use App\Enums\PlatformEnum;
6use App\Http\Resources\PlatformAccountResource;
7use App\Models\PlatformAccount;
8use App\Models\PlatformInstance;
9use Illuminate\Http\JsonResponse;
10use Illuminate\Http\Request;
11use Illuminate\Validation\ValidationException;
12
13class PlatformAccountsController extends BaseController
14{
15    /**
16     * Display a listing of platform accounts
17     */
18    public function index(): JsonResponse
19    {
20        $accounts = PlatformAccount::with(['platformInstance'])
21            ->orderBy('platform')
22            ->orderBy('created_at', 'desc')
23            ->get();
24
25        return $this->sendResponse(
26            PlatformAccountResource::collection($accounts),
27            'Platform accounts retrieved successfully.'
28        );
29    }
30
31    /**
32     * Store a newly created platform account
33     */
34    public function store(Request $request): JsonResponse
35    {
36        try {
37            $validated = $request->validate([
38                'platform' => 'required|in:lemmy,mastodon,reddit',
39                'instance_url' => 'required|url',
40                'username' => 'required|string|max:255',
41                'password' => 'required|string',
42                'settings' => 'nullable|array',
43            ]);
44
45            // Create or find platform instance
46            $platformEnum = PlatformEnum::from($validated['platform']);
47            $instance = PlatformInstance::firstOrCreate([
48                'platform' => $platformEnum,
49                'url' => $validated['instance_url'],
50            ], [
51                'name' => parse_url($validated['instance_url'], PHP_URL_HOST),
52                'description' => ucfirst($validated['platform']) . ' instance',
53                'is_active' => true,
54            ]);
55
56            $account = PlatformAccount::create($validated);
57
58            // If this is the first account for this platform, make it active
59            if (!PlatformAccount::where('platform', $validated['platform'])
60                ->where('is_active', true)
61                ->exists()) {
62                $account->setAsActive();
63            }
64
65            return $this->sendResponse(
66                new PlatformAccountResource($account->load('platformInstance')),
67                'Platform account created successfully!',
68                201
69            );
70        } catch (ValidationException $e) {
71            return $this->sendValidationError($e->errors());
72        } catch (\Exception $e) {
73            return $this->sendError('Failed to create platform account: ' . $e->getMessage(), [], 500);
74        }
75    }
76
77    /**
78     * Display the specified platform account
79     */
80    public function show(PlatformAccount $platformAccount): JsonResponse
81    {
82        return $this->sendResponse(
83            new PlatformAccountResource($platformAccount->load('platformInstance')),
84            'Platform account retrieved successfully.'
85        );
86    }
87
88    /**
89     * Update the specified platform account
90     */
91    public function update(Request $request, PlatformAccount $platformAccount): JsonResponse
92    {
93        try {
94            $validated = $request->validate([
95                'instance_url' => 'required|url',
96                'username' => 'required|string|max:255',
97                'password' => 'nullable|string',
98                'settings' => 'nullable|array',
99            ]);
100
101            // Don't update password if not provided
102            if (empty($validated['password'])) {
103                unset($validated['password']);
104            }
105
106            $platformAccount->update($validated);
107
108            return $this->sendResponse(
109                new PlatformAccountResource($platformAccount->fresh(['platformInstance'])),
110                'Platform account updated successfully!'
111            );
112        } catch (ValidationException $e) {
113            return $this->sendValidationError($e->errors());
114        } catch (\Exception $e) {
115            return $this->sendError('Failed to update platform account: ' . $e->getMessage(), [], 500);
116        }
117    }
118
119    /**
120     * Remove the specified platform account
121     */
122    public function destroy(PlatformAccount $platformAccount): JsonResponse
123    {
124        try {
125            $platformAccount->delete();
126
127            return $this->sendResponse(
128                null,
129                'Platform account deleted successfully!'
130            );
131        } catch (\Exception $e) {
132            return $this->sendError('Failed to delete platform account: ' . $e->getMessage(), [], 500);
133        }
134    }
135
136    /**
137     * Set platform account as active
138     */
139    public function setActive(PlatformAccount $platformAccount): JsonResponse
140    {
141        try {
142            $platformAccount->setAsActive();
143
144            return $this->sendResponse(
145                new PlatformAccountResource($platformAccount->fresh(['platformInstance'])),
146                "Set {$platformAccount->username}@{$platformAccount->instance_url} as active for {$platformAccount->platform->value}!"
147            );
148        } catch (\Exception $e) {
149            return $this->sendError('Failed to set platform account as active: ' . $e->getMessage(), [], 500);
150        }
151    }
152}