Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
PlatformChannelsController
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 6
182
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 / 19
0.00% covered (danger)
0.00%
0 / 1
12
 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 / 15
0.00% covered (danger)
0.00%
0 / 1
12
 destroy
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 toggle
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace App\Http\Controllers\Api\V1;
4
5use App\Http\Resources\PlatformChannelResource;
6use App\Models\PlatformChannel;
7use Illuminate\Http\JsonResponse;
8use Illuminate\Http\Request;
9use Illuminate\Validation\ValidationException;
10
11class PlatformChannelsController extends BaseController
12{
13    /**
14     * Display a listing of platform channels
15     */
16    public function index(): JsonResponse
17    {
18        $channels = PlatformChannel::with(['platformInstance'])
19            ->orderBy('is_active', 'desc')
20            ->orderBy('name')
21            ->get();
22
23        return $this->sendResponse(
24            PlatformChannelResource::collection($channels),
25            'Platform channels retrieved successfully.'
26        );
27    }
28
29    /**
30     * Store a newly created platform channel
31     */
32    public function store(Request $request): JsonResponse
33    {
34        try {
35            $validated = $request->validate([
36                'platform_instance_id' => 'required|exists:platform_instances,id',
37                'channel_id' => 'required|string|max:255',
38                'name' => 'required|string|max:255',
39                'display_name' => 'nullable|string|max:255',
40                'description' => 'nullable|string',
41                'is_active' => 'boolean',
42            ]);
43
44            $validated['is_active'] = $validated['is_active'] ?? true;
45
46            $channel = PlatformChannel::create($validated);
47
48            return $this->sendResponse(
49                new PlatformChannelResource($channel->load('platformInstance')),
50                'Platform channel created successfully!',
51                201
52            );
53        } catch (ValidationException $e) {
54            return $this->sendValidationError($e->errors());
55        } catch (\Exception $e) {
56            return $this->sendError('Failed to create platform channel: ' . $e->getMessage(), [], 500);
57        }
58    }
59
60    /**
61     * Display the specified platform channel
62     */
63    public function show(PlatformChannel $channel): JsonResponse
64    {
65        return $this->sendResponse(
66            new PlatformChannelResource($channel->load('platformInstance')),
67            'Platform channel retrieved successfully.'
68        );
69    }
70
71    /**
72     * Update the specified platform channel
73     */
74    public function update(Request $request, PlatformChannel $channel): JsonResponse
75    {
76        try {
77            $validated = $request->validate([
78                'name' => 'required|string|max:255',
79                'display_name' => 'nullable|string|max:255',
80                'description' => 'nullable|string',
81                'is_active' => 'boolean',
82            ]);
83
84            $channel->update($validated);
85
86            return $this->sendResponse(
87                new PlatformChannelResource($channel->fresh(['platformInstance'])),
88                'Platform channel updated successfully!'
89            );
90        } catch (ValidationException $e) {
91            return $this->sendValidationError($e->errors());
92        } catch (\Exception $e) {
93            return $this->sendError('Failed to update platform channel: ' . $e->getMessage(), [], 500);
94        }
95    }
96
97    /**
98     * Remove the specified platform channel
99     */
100    public function destroy(PlatformChannel $channel): JsonResponse
101    {
102        try {
103            $channel->delete();
104
105            return $this->sendResponse(
106                null,
107                'Platform channel deleted successfully!'
108            );
109        } catch (\Exception $e) {
110            return $this->sendError('Failed to delete platform channel: ' . $e->getMessage(), [], 500);
111        }
112    }
113
114    /**
115     * Toggle platform channel active status
116     */
117    public function toggle(PlatformChannel $channel): JsonResponse
118    {
119        try {
120            $newStatus = !$channel->is_active;
121            $channel->update(['is_active' => $newStatus]);
122
123            $status = $newStatus ? 'activated' : 'deactivated';
124
125            return $this->sendResponse(
126                new PlatformChannelResource($channel->fresh(['platformInstance'])),
127                "Platform channel {$status} successfully!"
128            );
129        } catch (\Exception $e) {
130            return $this->sendError('Failed to toggle platform channel status: ' . $e->getMessage(), [], 500);
131        }
132    }
133}