Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| GlobalSettingsController | |
100.00% |
17 / 17 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| show | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| store | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| update | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| destroy | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\v2\Admin; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use App\Http\Requests\v2\GlobalSettings\DestroyGlobalSettingsRequest; |
| 7 | use App\Http\Requests\v2\GlobalSettings\ShowGlobalSettingsRequest; |
| 8 | use App\Http\Requests\v2\GlobalSettings\StoreGlobalSettingsRequest; |
| 9 | use App\Http\Requests\v2\GlobalSettings\UpdateGlobalSettingsRequest; |
| 10 | use App\Http\Resources\v2\GlobalSettingsResource; |
| 11 | use App\Http\Services\GlobalSettingsService; |
| 12 | use Illuminate\Http\JsonResponse; |
| 13 | |
| 14 | /** |
| 15 | * Controller for managing global application settings. |
| 16 | * |
| 17 | * Only accessible by users with the VENGRESO_ADMIN role. |
| 18 | * Global settings is a single record that contains application-wide configuration defaults. |
| 19 | * |
| 20 | * Unlike typical CRUD controllers, this manages a single record: |
| 21 | * - GET: Retrieve the global settings |
| 22 | * - PUT: Update the global settings (partial update supported) |
| 23 | * - POST: Create/reset global settings to defaults |
| 24 | * - DELETE: Delete the global settings |
| 25 | */ |
| 26 | class GlobalSettingsController extends Controller |
| 27 | { |
| 28 | public function __construct( |
| 29 | private GlobalSettingsService $globalSettingsService |
| 30 | ) {} |
| 31 | |
| 32 | /** |
| 33 | * Get the global settings. |
| 34 | * |
| 35 | * @param ShowGlobalSettingsRequest $request Validated request (authorization only) |
| 36 | * |
| 37 | * @response 200 { |
| 38 | * "result": { |
| 39 | * "data": { |
| 40 | * "id": "507f1f77bcf86cd799439011", |
| 41 | * "user_id": "app_settings", |
| 42 | * "is_global": true, |
| 43 | * "wage_per_hour": 31.65, |
| 44 | * "words_per_minute": 40, |
| 45 | * "typing_style": "all", |
| 46 | * "typing_speed": 45, |
| 47 | * "remove_grammar_on_lose_focus": false, |
| 48 | * "fly_rewrite": "enabled", |
| 49 | * "fly_grammar": "enabled", |
| 50 | * "fly_grammar_default_language": "en_US", |
| 51 | * "features": {...}, |
| 52 | * "playlists": {...}, |
| 53 | * "ignoredInputTypes": [...], |
| 54 | * "blackListClasses": [...], |
| 55 | * "blackListIds": [...], |
| 56 | * "blackListAttributes": [...], |
| 57 | * "blackListDomainRules": [...], |
| 58 | * "created_at": 1642521600, |
| 59 | * "updated_at": 1642521600 |
| 60 | * } |
| 61 | * } |
| 62 | * } |
| 63 | * @response 404 {"error": "Global settings not found"} |
| 64 | */ |
| 65 | public function show(ShowGlobalSettingsRequest $request): GlobalSettingsResource|JsonResponse |
| 66 | { |
| 67 | $settings = $this->globalSettingsService->get(); |
| 68 | |
| 69 | if (! $settings) { |
| 70 | return response()->json(['error' => 'Global settings not found'], 404); |
| 71 | } |
| 72 | |
| 73 | return new GlobalSettingsResource($settings); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Create or reset global settings to defaults. |
| 78 | * |
| 79 | * If global settings already exist, they will be deleted and recreated with defaults. |
| 80 | * Any provided data will be merged with the defaults. |
| 81 | * |
| 82 | * @param StoreGlobalSettingsRequest $request Validated request with optional custom values |
| 83 | * |
| 84 | * @response 201 { |
| 85 | * "result": { |
| 86 | * "data": { |
| 87 | * "id": "507f1f77bcf86cd799439011", |
| 88 | * "user_id": "app_settings", |
| 89 | * "is_global": true, |
| 90 | * ... |
| 91 | * } |
| 92 | * } |
| 93 | * } |
| 94 | */ |
| 95 | public function store(StoreGlobalSettingsRequest $request): JsonResponse |
| 96 | { |
| 97 | $settings = $this->globalSettingsService->create($request->validated()); |
| 98 | |
| 99 | return (new GlobalSettingsResource($settings)) |
| 100 | ->response() |
| 101 | ->setStatusCode(201); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Update the global settings (partial update supported). |
| 106 | * |
| 107 | * Only provided fields will be updated. For nested objects like 'features', |
| 108 | * provided values are merged with existing values rather than replacing entirely. |
| 109 | * |
| 110 | * @param UpdateGlobalSettingsRequest $request Validated request with fields to update |
| 111 | * |
| 112 | * @response 200 { |
| 113 | * "result": { |
| 114 | * "data": { |
| 115 | * "id": "507f1f77bcf86cd799439011", |
| 116 | * "user_id": "app_settings", |
| 117 | * "is_global": true, |
| 118 | * "wage_per_hour": 35.00, |
| 119 | * ... |
| 120 | * } |
| 121 | * } |
| 122 | * } |
| 123 | * @response 404 {"error": "Global settings not found"} |
| 124 | */ |
| 125 | public function update(UpdateGlobalSettingsRequest $request): GlobalSettingsResource|JsonResponse |
| 126 | { |
| 127 | $settings = $this->globalSettingsService->update($request->validated()); |
| 128 | |
| 129 | if (! $settings) { |
| 130 | return response()->json(['error' => 'Global settings not found'], 404); |
| 131 | } |
| 132 | |
| 133 | return new GlobalSettingsResource($settings); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Delete the global settings. |
| 138 | * |
| 139 | * @param DestroyGlobalSettingsRequest $request Validated request (authorization only) |
| 140 | * |
| 141 | * @response 204 No content |
| 142 | * @response 404 {"error": "Global settings not found"} |
| 143 | */ |
| 144 | public function destroy(DestroyGlobalSettingsRequest $request): JsonResponse |
| 145 | { |
| 146 | $deleted = $this->globalSettingsService->delete(); |
| 147 | |
| 148 | if (! $deleted) { |
| 149 | return response()->json(['error' => 'Global settings not found'], 404); |
| 150 | } |
| 151 | |
| 152 | return response()->json(null, 204); |
| 153 | } |
| 154 | } |