Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
RemoteConfigRepository
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
5 / 5
6
100.00% covered (success)
100.00%
1 / 1
 getCurrent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 upsert
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getHistory
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getHistoryByVersion
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createHistoryEntry
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Http\Repositories;
4
5use App\Http\Models\RemoteConfig;
6use App\Http\Models\RemoteConfigHistory;
7use App\Http\Repositories\interfaces\IRemoteConfigRepository;
8use Illuminate\Contracts\Pagination\LengthAwarePaginator;
9
10/**
11 * Repository for remote config data access operations.
12 *
13 * Handles all database queries related to remote configuration,
14 * keeping data access logic separate from business logic.
15 */
16class RemoteConfigRepository implements IRemoteConfigRepository
17{
18    /**
19     * Get the current active remote config.
20     *
21     * @return RemoteConfig|null The current config or null if none exists
22     */
23    public function getCurrent(): ?RemoteConfig
24    {
25        return RemoteConfig::first();
26    }
27
28    /**
29     * Create or update the single-row remote config.
30     *
31     * @param  array<string, mixed>  $data  The config data to upsert
32     * @return RemoteConfig The created or updated config
33     */
34    public function upsert(array $data): RemoteConfig
35    {
36        $config = RemoteConfig::first();
37
38        if ($config) {
39            $config->update($data);
40
41            return $config->fresh();
42        }
43
44        return RemoteConfig::create($data);
45    }
46
47    /**
48     * Get paginated history entries, newest first.
49     *
50     * @param  int  $perPage  Number of items per page
51     * @return LengthAwarePaginator<RemoteConfigHistory>
52     */
53    public function getHistory(int $perPage = 20): LengthAwarePaginator
54    {
55        return RemoteConfigHistory::orderBy('created_at', 'desc')
56            ->paginate($perPage);
57    }
58
59    /**
60     * Find a history entry by its version string.
61     *
62     * @param  string  $version  The version identifier
63     * @return RemoteConfigHistory|null The history entry or null
64     */
65    public function getHistoryByVersion(string $version): ?RemoteConfigHistory
66    {
67        return RemoteConfigHistory::where('version', $version)->first();
68    }
69
70    /**
71     * Create a new history entry.
72     *
73     * @param  array<string, mixed>  $snapshot  The full config snapshot
74     * @param  string  $changedBy  Who made the change
75     * @param  string|null  $description  Description of the change
76     * @return RemoteConfigHistory The created history entry
77     */
78    public function createHistoryEntry(array $snapshot, string $changedBy, ?string $description): RemoteConfigHistory
79    {
80        return RemoteConfigHistory::create([
81            'version' => $snapshot['version'] ?? now()->toIso8601String(),
82            'snapshot' => $snapshot,
83            'changed_by' => $changedBy,
84            'change_description' => $description,
85        ]);
86    }
87}