44
55namespace Pan \Adapters \Laravel \Repositories ;
66
7- use Illuminate \Support \Facades \DB ;
7+ use Illuminate \Database \Connection ;
8+ use Illuminate \Database \DatabaseManager ;
89use Pan \Contracts \AnalyticsRepository ;
910use Pan \Enums \EventType ;
1011use Pan \PanConfiguration ;
1819 /**
1920 * Creates a new analytics repository instance.
2021 */
21- public function __construct (private PanConfiguration $ config )
22- {
23- //
24- }
22+ public function __construct (
23+ private DatabaseManager $ databaseManager ,
24+ private PanConfiguration $ config
25+ ) { }
2526
2627 /**
2728 * Returns all analytics.
@@ -31,7 +32,7 @@ public function __construct(private PanConfiguration $config)
3132 public function all (): array
3233 {
3334 /** @var array<int, Analytic> $all */
34- $ all = DB :: table ('pan_analytics ' )->get ()->map (fn (mixed $ analytic ): Analytic => new Analytic (
35+ $ all = $ this -> connection ()-> table ('pan_analytics ' )->get ()->map (fn (mixed $ analytic ): Analytic => new Analytic (
3536 id: (int ) $ analytic ->id ,
3637 name: $ analytic ->name ,
3738 impressions: (int ) $ analytic ->impressions ,
@@ -56,22 +57,30 @@ public function increment(string $name, EventType $event): void
5657 return ;
5758 }
5859
59- if (DB :: table ('pan_analytics ' )->where ('name ' , $ name )->count () === 0 ) {
60- if (DB :: table ('pan_analytics ' )->count () < $ maxAnalytics ) {
61- DB :: table ('pan_analytics ' )->insert (['name ' => $ name , $ event ->column () => 1 ]);
60+ if ($ this -> connection ()-> table ('pan_analytics ' )->where ('name ' , $ name )->count () === 0 ) {
61+ if ($ this -> connection ()-> table ('pan_analytics ' )->count () < $ maxAnalytics ) {
62+ $ this -> connection ()-> table ('pan_analytics ' )->insert (['name ' => $ name , $ event ->column () => 1 ]);
6263 }
6364
6465 return ;
6566 }
6667
67- DB :: table ('pan_analytics ' )->where ('name ' , $ name )->increment ($ event ->column ());
68+ $ this -> connection ()-> table ('pan_analytics ' )->where ('name ' , $ name )->increment ($ event ->column ());
6869 }
6970
7071 /**
7172 * Flush all analytics.
7273 */
7374 public function flush (): void
7475 {
75- DB ::table ('pan_analytics ' )->truncate ();
76+ $ this ->connection ()->table ('pan_analytics ' )->truncate ();
77+ }
78+
79+ /**
80+ * Resolve the database connection.
81+ */
82+ private function connection (): Connection
83+ {
84+ return $ this ->databaseManager ->connection ($ this ->config ->getDatabaseConnection ());
7685 }
7786}
0 commit comments