43 lines
1.5 KiB
PHP
43 lines
1.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Modules\FxRates\Infrastructure;
|
|
|
|
use App\PdoFactory;
|
|
use RuntimeException;
|
|
use PDO;
|
|
|
|
final class ConnectionFactory
|
|
{
|
|
public static function make(SettingsStore $settingsStore): PDO
|
|
{
|
|
$settings = $settingsStore->load();
|
|
$useSeparateDb = !empty($settings['use_separate_db']);
|
|
|
|
if ($useSeparateDb) {
|
|
$dbConfig = is_array($settings['db'] ?? null) ? $settings['db'] : [];
|
|
if ($dbConfig === []) {
|
|
throw new RuntimeException('Custom-Datenbank ist aktiviert, aber nicht vollstaendig konfiguriert.');
|
|
}
|
|
self::assertSupportedDriver($dbConfig);
|
|
return PdoFactory::createFromArray($dbConfig);
|
|
}
|
|
|
|
$dbConfig = app()->config()->dbConfig;
|
|
if ($dbConfig === []) {
|
|
throw new RuntimeException('Projekt-Datenbankkonfiguration fehlt in config/db_settings_basic.php.');
|
|
}
|
|
|
|
self::assertSupportedDriver($dbConfig);
|
|
return PdoFactory::createFromArray($dbConfig);
|
|
}
|
|
|
|
private static function assertSupportedDriver(array $dbConfig): void
|
|
{
|
|
$driver = strtolower((string) ($dbConfig['driver'] ?? ($dbConfig['dsn'] ?? '')));
|
|
if ($driver !== '' && !in_array($driver, ['mysql', 'pgsql', 'sqlite'], true) && !str_starts_with($driver, 'mysql:') && !str_starts_with($driver, 'pgsql:') && !str_starts_with($driver, 'sqlite:')) {
|
|
throw new RuntimeException('FX-Rates unterstuetzt aktuell MySQL/MariaDB, PostgreSQL und SQLite.');
|
|
}
|
|
}
|
|
}
|