Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
75.00% |
9 / 12 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| GlobalSettingsRepository | |
75.00% |
9 / 12 |
|
80.00% |
4 / 5 |
5.39 | |
0.00% |
0 / 1 |
| find | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| exists | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| create | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| update | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| delete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Repositories; |
| 4 | |
| 5 | use App\Http\Models\Setting; |
| 6 | |
| 7 | /** |
| 8 | * Repository for global settings data access operations. |
| 9 | * |
| 10 | * This repository handles all database queries related to the global settings, |
| 11 | * keeping data access logic separate from business logic. Global settings |
| 12 | * is a single record identified by user_id = 'app_settings'. |
| 13 | */ |
| 14 | class GlobalSettingsRepository |
| 15 | { |
| 16 | /** |
| 17 | * Find the global settings record. |
| 18 | * |
| 19 | * @return Setting|null The global settings or null if not found |
| 20 | */ |
| 21 | public function find(): ?Setting |
| 22 | { |
| 23 | return Setting::where('user_id', Setting::GLOBAL_USER_ID) |
| 24 | ->where('is_global', true) |
| 25 | ->first(); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Check if global settings exist. |
| 30 | * |
| 31 | * @return bool True if global settings exist |
| 32 | */ |
| 33 | public function exists(): bool |
| 34 | { |
| 35 | return Setting::where('user_id', Setting::GLOBAL_USER_ID) |
| 36 | ->where('is_global', true) |
| 37 | ->exists(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Create new global settings. |
| 42 | * |
| 43 | * @param array<string, mixed> $data The settings data |
| 44 | * @return Setting The created settings |
| 45 | */ |
| 46 | public function create(array $data): Setting |
| 47 | { |
| 48 | // Ensure global settings identifiers are set |
| 49 | $data['user_id'] = Setting::GLOBAL_USER_ID; |
| 50 | $data['is_global'] = true; |
| 51 | |
| 52 | return Setting::create($data); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Update the global settings. |
| 57 | * |
| 58 | * @param Setting $setting The settings to update |
| 59 | * @param array<string, mixed> $data The update data |
| 60 | * @return Setting The updated settings |
| 61 | */ |
| 62 | public function update(Setting $setting, array $data): Setting |
| 63 | { |
| 64 | $setting->update($data); |
| 65 | |
| 66 | return $setting->fresh(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Delete the global settings. |
| 71 | * |
| 72 | * @param Setting $setting The settings to delete |
| 73 | * @return bool True if deleted successfully |
| 74 | */ |
| 75 | public function delete(Setting $setting): bool |
| 76 | { |
| 77 | return $setting->delete(); |
| 78 | } |
| 79 | } |