Skip to content

Commit fd943cb

Browse files
authored
Merge pull request #49 from patressz/feat/custom-db-connection
feat: add ability to configure custom `database` connection
2 parents 16c2249 + d83b167 commit fd943cb

3 files changed

Lines changed: 55 additions & 11 deletions

File tree

src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
namespace Pan\Adapters\Laravel\Repositories;
66

7-
use Illuminate\Support\Facades\DB;
7+
use Illuminate\Database\Connection;
8+
use Illuminate\Database\DatabaseManager;
89
use Pan\Contracts\AnalyticsRepository;
910
use Pan\Enums\EventType;
1011
use Pan\PanConfiguration;
@@ -18,10 +19,10 @@
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
}

src/PanConfiguration.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ private function __construct(
2020
private int $maxAnalytics = 50,
2121
private array $allowedAnalytics = [],
2222
private string $routePrefix = 'pan',
23+
private ?string $databaseConnection = null,
2324
) {
2425
//
2526
}
@@ -66,6 +67,24 @@ public function setRoutePrefix(string $prefix): void
6667
$this->routePrefix = $prefix;
6768
}
6869

70+
/**
71+
* Sets the database connection to be used.
72+
*
73+
* @internal
74+
*/
75+
public function setDatabaseConnection(string $connection): void
76+
{
77+
$this->databaseConnection = $connection;
78+
}
79+
80+
/**
81+
* Sets the database connection to be used.
82+
*/
83+
public static function databaseConnection(string $connection): void
84+
{
85+
self::instance()->setDatabaseConnection($connection);
86+
}
87+
6988
/**
7089
* Sets the maximum number of analytics to store.
7190
*/
@@ -129,4 +148,14 @@ public function toArray(): array
129148
'route_prefix' => $this->routePrefix,
130149
];
131150
}
151+
152+
/**
153+
* Get the database connection to be used.
154+
*
155+
* @internal
156+
*/
157+
public function getDatabaseConnection(): ?string
158+
{
159+
return $this->databaseConnection;
160+
}
132161
}

tests/Unit/PanConfigurationTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,9 @@
7777
'route_prefix' => 'pan',
7878
]);
7979
});
80+
81+
it('can set the database connection', function (): void {
82+
PanConfiguration::databaseConnection('sqlite');
83+
84+
expect(PanConfiguration::instance()->getDatabaseConnection())->toBe('sqlite');
85+
});

0 commit comments

Comments
 (0)