Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
60.00% |
6 / 10 |
|
50.00% |
3 / 6 |
CRAP | |
0.00% |
0 / 1 |
| UserSettingRepository | |
60.00% |
6 / 10 |
|
50.00% |
3 / 6 |
10.14 | |
0.00% |
0 / 1 |
| findByUserId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| updateByUserId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| findRawByUserId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| findOrCreateRawByUserId | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
2.26 | |||
| findByCompanyId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| findGlobalSettings | |
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 | use Illuminate\Support\Facades\DB; |
| 7 | use stdClass; |
| 8 | |
| 9 | /** |
| 10 | * Repository for user settings data access operations. |
| 11 | * |
| 12 | * This repository handles all database queries related to user-specific settings, |
| 13 | * keeping data access logic separate from business logic. |
| 14 | */ |
| 15 | class UserSettingRepository |
| 16 | { |
| 17 | /** |
| 18 | * Find a user's setting record by user ID. |
| 19 | * |
| 20 | * @param string $userId The user ID |
| 21 | * @return Setting|null The user's setting or null if not found |
| 22 | */ |
| 23 | public function findByUserId(string $userId): ?Setting |
| 24 | { |
| 25 | return Setting::where('user_id', $userId)->first(); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Update a user's setting record by user ID. |
| 30 | * |
| 31 | * @param string $userId The user ID |
| 32 | * @param array<string, mixed> $data The data to update |
| 33 | * @return int The number of affected rows |
| 34 | */ |
| 35 | public function updateByUserId(string $userId, array $data): int |
| 36 | { |
| 37 | return Setting::where('user_id', $userId)->update($data); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Find a user's raw setting record by user ID. |
| 42 | * |
| 43 | * Uses DB::table() to return a stdClass object, preserving the exact |
| 44 | * merge behavior used in the settings details endpoint. |
| 45 | * |
| 46 | * @param string $userId The user ID |
| 47 | * @return stdClass|null The raw setting record or null if not found |
| 48 | */ |
| 49 | public function findRawByUserId(string $userId): ?stdClass |
| 50 | { |
| 51 | return DB::table('settings')->where('user_id', $userId)->first(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Find a user's raw setting record by user ID, creating a default record if none exists. |
| 56 | * |
| 57 | * Uses DB::table() to return a stdClass object, preserving the exact |
| 58 | * merge behavior used in the settings details endpoint. If the user has |
| 59 | * no settings record yet, a minimal default record is created to prevent |
| 60 | * null reference errors on subsequent property assignments. |
| 61 | * |
| 62 | * @param string $userId The user ID |
| 63 | * @return stdClass The raw setting record (never null) |
| 64 | */ |
| 65 | public function findOrCreateRawByUserId(string $userId): stdClass |
| 66 | { |
| 67 | $result = DB::table('settings')->where('user_id', $userId)->first(); |
| 68 | |
| 69 | if (! $result) { |
| 70 | Setting::create(['user_id' => $userId]); |
| 71 | |
| 72 | $result = DB::table('settings')->where('user_id', $userId)->first(); |
| 73 | } |
| 74 | |
| 75 | return $result; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Find a company's setting record by company ID. |
| 80 | * |
| 81 | * @param string $companyId The company ID |
| 82 | * @return Setting|null The company's setting or null if not found |
| 83 | */ |
| 84 | public function findByCompanyId(string $companyId): ?Setting |
| 85 | { |
| 86 | return Setting::where('company_id', $companyId)->first(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Find the global settings record. |
| 91 | * |
| 92 | * @return Setting|null The global settings or null if not found |
| 93 | */ |
| 94 | public function findGlobalSettings(): ?Setting |
| 95 | { |
| 96 | return Setting::where('user_id', Setting::GLOBAL_USER_ID)->first(); |
| 97 | } |
| 98 | } |