From ecdc4e748264d590b545557b9574dbe40a667990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrik=20Stri=C5=A1ovsk=C3=BD?= Date: Fri, 7 Feb 2025 16:33:49 +0100 Subject: [PATCH] feat: add ability to configure custom `database` connection --- .../DatabaseAnalyticsRepository.php | 41 +++++++++++-------- src/PanConfiguration.php | 29 +++++++++++++ tests/Unit/PanConfigurationTest.php | 6 +++ 3 files changed, 60 insertions(+), 16 deletions(-) diff --git a/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php b/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php index 7e3d1da..abac116 100644 --- a/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php +++ b/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php @@ -4,7 +4,8 @@ namespace Pan\Adapters\Laravel\Repositories; -use Illuminate\Support\Facades\DB; +use Illuminate\Database\Connection; +use Illuminate\Database\DatabaseManager; use Pan\Contracts\AnalyticsRepository; use Pan\Enums\EventType; use Pan\PanConfiguration; @@ -18,10 +19,10 @@ /** * Creates a new analytics repository instance. */ - public function __construct(private PanConfiguration $config) - { - // - } + public function __construct( + private DatabaseManager $db, + private PanConfiguration $config + ) {} /** * Returns all analytics. @@ -31,12 +32,12 @@ public function __construct(private PanConfiguration $config) public function all(): array { /** @var array $all */ - $all = DB::table('pan_analytics')->get()->map(fn (mixed $analytic): Analytic => new Analytic( - id: (int) $analytic->id, // @phpstan-ignore-line - name: $analytic->name, // @phpstan-ignore-line - impressions: (int) $analytic->impressions, // @phpstan-ignore-line - hovers: (int) $analytic->hovers, // @phpstan-ignore-line - clicks: (int) $analytic->clicks, // @phpstan-ignore-line + $all = $this->connection()->table('pan_analytics')->get()->map(fn (mixed $analytic): Analytic => new Analytic( + id: (int) $analytic->id, + name: $analytic->name, + impressions: (int) $analytic->impressions, + hovers: (int) $analytic->hovers, + clicks: (int) $analytic->clicks, ))->toArray(); return $all; @@ -56,15 +57,15 @@ public function increment(string $name, EventType $event): void return; } - if (DB::table('pan_analytics')->where('name', $name)->count() === 0) { - if (DB::table('pan_analytics')->count() < $maxAnalytics) { - DB::table('pan_analytics')->insert(['name' => $name, $event->column() => 1]); + if ($this->connection()->table('pan_analytics')->where('name', $name)->count() === 0) { + if ($this->connection()->table('pan_analytics')->count() < $maxAnalytics) { + $this->connection()->table('pan_analytics')->insert(['name' => $name, $event->column() => 1]); } return; } - DB::table('pan_analytics')->where('name', $name)->increment($event->column()); + $this->connection()->table('pan_analytics')->where('name', $name)->increment($event->column()); } /** @@ -72,6 +73,14 @@ public function increment(string $name, EventType $event): void */ public function flush(): void { - DB::table('pan_analytics')->truncate(); + $this->connection()->table('pan_analytics')->truncate(); + } + + /** + * Resolve the database connection. + */ + private function connection(): Connection + { + return $this->db->connection($this->config->getDatabaseConnection()); } } diff --git a/src/PanConfiguration.php b/src/PanConfiguration.php index b70b4a1..2cb220f 100644 --- a/src/PanConfiguration.php +++ b/src/PanConfiguration.php @@ -20,6 +20,7 @@ private function __construct( private int $maxAnalytics = 50, private array $allowedAnalytics = [], private string $routePrefix = 'pan', + private ?string $databaseConnection = null, ) { // } @@ -66,6 +67,24 @@ public function setRoutePrefix(string $prefix): void $this->routePrefix = $prefix; } + /** + * Sets the database connection to be used. + * + * @internal + */ + public function setDatabaseConnection(string $connection): void + { + $this->databaseConnection = $connection; + } + + /** + * Sets the database connection to be used. + */ + public static function databaseConnection(string $connection): void + { + self::instance()->setDatabaseConnection($connection); + } + /** * Sets the maximum number of analytics to store. */ @@ -129,4 +148,14 @@ public function toArray(): array 'route_prefix' => $this->routePrefix, ]; } + + /** + * Get the database connection to be used. + * + * @internal + */ + public function getDatabaseConnection(): ?string + { + return $this->databaseConnection; + } } diff --git a/tests/Unit/PanConfigurationTest.php b/tests/Unit/PanConfigurationTest.php index daf4bda..ce1270a 100644 --- a/tests/Unit/PanConfigurationTest.php +++ b/tests/Unit/PanConfigurationTest.php @@ -77,3 +77,9 @@ 'route_prefix' => 'pan', ]); }); + +it('can set the database connection', function (): void { + PanConfiguration::databaseConnection('sqlite'); + + expect(PanConfiguration::instance()->getDatabaseConnection())->toBe('sqlite'); +});