Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to configure custom database connection #49

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -31,12 +32,12 @@ public function __construct(private PanConfiguration $config)
public function all(): array
{
/** @var array<int, Analytic> $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;
Expand All @@ -56,22 +57,30 @@ 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());
}

/**
* Flush all analytics.
*/
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());
}
}
29 changes: 29 additions & 0 deletions src/PanConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ private function __construct(
private int $maxAnalytics = 50,
private array $allowedAnalytics = [],
private string $routePrefix = 'pan',
private ?string $databaseConnection = null,
) {
//
}
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
}
}
6 changes: 6 additions & 0 deletions tests/Unit/PanConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,9 @@
'route_prefix' => 'pan',
]);
});

it('can set the database connection', function (): void {
PanConfiguration::databaseConnection('sqlite');

expect(PanConfiguration::instance()->getDatabaseConnection())->toBe('sqlite');
});