Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| UserFieldInjectionRepository | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| findByUserId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| upsert | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| deleteByUserId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Repositories; |
| 4 | |
| 5 | use App\Http\Models\UserFieldInjection; |
| 6 | use App\Http\Repositories\interfaces\IUserFieldInjectionRepository; |
| 7 | |
| 8 | /** |
| 9 | * Repository for user field injection data access operations. |
| 10 | * |
| 11 | * Handles all database queries related to per-user field injection customizations, |
| 12 | * keeping data access logic separate from business logic. |
| 13 | */ |
| 14 | class UserFieldInjectionRepository implements IUserFieldInjectionRepository |
| 15 | { |
| 16 | /** |
| 17 | * Find a user's field injection config. |
| 18 | * |
| 19 | * @param string $userId The user ID |
| 20 | * @return UserFieldInjection|null The user's config or null if none exists |
| 21 | */ |
| 22 | public function findByUserId(string $userId): ?UserFieldInjection |
| 23 | { |
| 24 | return UserFieldInjection::where('user_id', $userId)->first(); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Create or update a user's field injection config. |
| 29 | * |
| 30 | * @param string $userId The user ID |
| 31 | * @param array<string, mixed> $data The data to upsert |
| 32 | * @return UserFieldInjection The created or updated config |
| 33 | */ |
| 34 | public function upsert(string $userId, array $data): UserFieldInjection |
| 35 | { |
| 36 | $config = UserFieldInjection::where('user_id', $userId)->first(); |
| 37 | |
| 38 | if ($config) { |
| 39 | $config->update($data); |
| 40 | |
| 41 | return $config->fresh(); |
| 42 | } |
| 43 | |
| 44 | return UserFieldInjection::create(array_merge(['user_id' => $userId], $data)); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Delete a user's field injection config entirely. |
| 49 | * |
| 50 | * @param string $userId The user ID |
| 51 | * @return bool True if deleted, false if not found |
| 52 | */ |
| 53 | public function deleteByUserId(string $userId): bool |
| 54 | { |
| 55 | $deleted = UserFieldInjection::where('user_id', $userId)->delete(); |
| 56 | |
| 57 | return $deleted > 0; |
| 58 | } |
| 59 | } |