Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SettingsController
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 index
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 update
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace App\Http\Controllers\Api\V1;
4
5use App\Models\Setting;
6use Illuminate\Http\JsonResponse;
7use Illuminate\Http\Request;
8use Illuminate\Validation\ValidationException;
9
10class SettingsController extends BaseController
11{
12    /**
13     * Display current settings
14     */
15    public function index(): JsonResponse
16    {
17        try {
18            $settings = [
19                'article_processing_enabled' => Setting::isArticleProcessingEnabled(),
20                'publishing_approvals_enabled' => Setting::isPublishingApprovalsEnabled(),
21            ];
22
23            return $this->sendResponse($settings, 'Settings retrieved successfully.');
24        } catch (\Exception $e) {
25            return $this->sendError('Failed to retrieve settings: ' . $e->getMessage(), [], 500);
26        }
27    }
28
29    /**
30     * Update settings
31     */
32    public function update(Request $request): JsonResponse
33    {
34        try {
35            $validated = $request->validate([
36                'article_processing_enabled' => 'boolean',
37                'enable_publishing_approvals' => 'boolean',
38            ]);
39
40            if (isset($validated['article_processing_enabled'])) {
41                Setting::setArticleProcessingEnabled($validated['article_processing_enabled']);
42            }
43
44            if (isset($validated['enable_publishing_approvals'])) {
45                Setting::setPublishingApprovalsEnabled($validated['enable_publishing_approvals']);
46            }
47
48            $updatedSettings = [
49                'article_processing_enabled' => Setting::isArticleProcessingEnabled(),
50                'publishing_approvals_enabled' => Setting::isPublishingApprovalsEnabled(),
51            ];
52
53            return $this->sendResponse(
54                $updatedSettings,
55                'Settings updated successfully.'
56            );
57        } catch (ValidationException $e) {
58            return $this->sendValidationError($e->errors());
59        } catch (\Exception $e) {
60            return $this->sendError('Failed to update settings: ' . $e->getMessage(), [], 500);
61        }
62    }
63}