From 55ea19ee91899fe8f70a40d09fdfe41c52575f36 Mon Sep 17 00:00:00 2001 From: guvra Date: Tue, 6 Feb 2024 16:18:24 +0100 Subject: [PATCH] Add database factory object --- app/config/services.yaml | 4 ++++ src/Database/DatabaseFactory.php | 31 +++++++++++++++++++++++++++++++ src/Dumper/MysqlDumper.php | 31 ++++--------------------------- 3 files changed, 39 insertions(+), 27 deletions(-) create mode 100644 src/Database/DatabaseFactory.php diff --git a/app/config/services.yaml b/app/config/services.yaml index deeb77b5..d0b61a68 100644 --- a/app/config/services.yaml +++ b/app/config/services.yaml @@ -70,10 +70,14 @@ services: converter.resolver: class: 'Smile\GdprDump\Converter\ConverterResolver' + database.factory: + class: 'Smile\GdprDump\Database\DatabaseFactory' + dumper: class: 'Smile\GdprDump\Dumper\MysqlDumper' public: true # used by functional tests arguments: + - '@database.factory' - !tagged_iterator dumper.mysqldump_extension dumper.data_converter_extension: diff --git a/src/Database/DatabaseFactory.php b/src/Database/DatabaseFactory.php new file mode 100644 index 00000000..3f42d072 --- /dev/null +++ b/src/Database/DatabaseFactory.php @@ -0,0 +1,31 @@ +get('database', []); + + // Rename some keys (for compatibility with the Doctrine connection) + if (array_key_exists('name', $connectionParams)) { + $connectionParams['dbname'] = $connectionParams['name']; + unset($connectionParams['name']); + } + + if (array_key_exists('driver_options', $connectionParams)) { + $connectionParams['driverOptions'] = $connectionParams['driver_options']; + unset($connectionParams['driver_options']); + } + + return new Database($connectionParams); + } +} diff --git a/src/Dumper/MysqlDumper.php b/src/Dumper/MysqlDumper.php index 57aacf42..8fd1c9f1 100644 --- a/src/Dumper/MysqlDumper.php +++ b/src/Dumper/MysqlDumper.php @@ -4,10 +4,9 @@ namespace Smile\GdprDump\Dumper; -use Doctrine\DBAL\Exception as DBALException; use Druidfi\Mysqldump\Mysqldump; use Smile\GdprDump\Config\ConfigInterface; -use Smile\GdprDump\Database\Database; +use Smile\GdprDump\Database\DatabaseFactory; use Smile\GdprDump\Dumper\Config\ConfigProcessor; use Smile\GdprDump\Dumper\Config\DumperConfig; use Smile\GdprDump\Dumper\Mysql\Context; @@ -18,7 +17,7 @@ class MysqlDumper implements DumperInterface /** * @param ExtensionInterface[] $extensions */ - public function __construct(private iterable $extensions = []) + public function __construct(private DatabaseFactory $databaseFactory, private iterable $extensions = []) { } @@ -27,8 +26,9 @@ public function __construct(private iterable $extensions = []) */ public function dump(ConfigInterface $config): void { + $database = $this->databaseFactory->create($config); + // Process the configuration - $database = $this->getDatabase($config); $processor = new ConfigProcessor($database->getMetadata()); $config = $processor->process($config); @@ -66,29 +66,6 @@ public function dump(ConfigInterface $config): void $dumper->start($output); } - /** - * Create a database object. - * - * @throws DBALException - */ - private function getDatabase(ConfigInterface $config): Database - { - $connectionParams = $config->get('database', []); - - // Rename some keys (for compatibility with the Doctrine connection) - if (array_key_exists('name', $connectionParams)) { - $connectionParams['dbname'] = $connectionParams['name']; - unset($connectionParams['name']); - } - - if (array_key_exists('driver_options', $connectionParams)) { - $connectionParams['driverOptions'] = $connectionParams['driver_options']; - unset($connectionParams['driver_options']); - } - - return new Database($connectionParams); - } - /** * Get the dump settings. */