From 39d6da533f77ba53267f502205b261584647ab93 Mon Sep 17 00:00:00 2001 From: guvra Date: Tue, 7 Jan 2025 16:06:09 +0100 Subject: [PATCH] Add database interface --- src/Database/Database.php | 21 ++++----------- src/Database/DatabaseInterface.php | 43 ++++++++++++++++++++++++++++++ src/Database/ParameterBag.php | 4 +-- src/Dumper/Event/DumpEvent.php | 6 ++--- 4 files changed, 53 insertions(+), 21 deletions(-) create mode 100644 src/Database/DatabaseInterface.php diff --git a/src/Database/Database.php b/src/Database/Database.php index 69ef735f..48b2b30b 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -13,19 +13,8 @@ use Smile\GdprDump\Database\Metadata\MysqlMetadata; use UnexpectedValueException; -/** - * Wrapper that stores the following objects: - * - connection: the Doctrine connection. - * - driver: allows to retrieve the DSN that was used to connect to the database. - * - metadata: allows to fetch the database metadata (table names, foreign key constraints). - * - * We use a custom abstraction layer for database metadata, because the Doctrine schema manager - * crashes when used with databases that use custom Doctrine types (e.g. OroCommerce). - */ -final class Database +final class Database implements DatabaseInterface { - public const DRIVER_MYSQL = 'pdo_mysql'; - private Connection $connection; private DriverInterface $driver; private MetadataInterface $metadata; @@ -53,7 +42,7 @@ public function __construct(array $connectionParams) } /** - * Get the doctrine connection. + * @inheritdoc */ public function getConnection(): Connection { @@ -61,7 +50,7 @@ public function getConnection(): Connection } /** - * Get the database driver. + * @inheritdoc */ public function getDriver(): DriverInterface { @@ -69,7 +58,7 @@ public function getDriver(): DriverInterface } /** - * Get the database metadata. + * @inheritdoc */ public function getMetadata(): MetadataInterface { @@ -77,7 +66,7 @@ public function getMetadata(): MetadataInterface } /** - * Get the connection parameters (host, port, user...). + * @inheritdoc */ public function getConnectionParams(): ParameterBag { diff --git a/src/Database/DatabaseInterface.php b/src/Database/DatabaseInterface.php new file mode 100644 index 00000000..861d71be --- /dev/null +++ b/src/Database/DatabaseInterface.php @@ -0,0 +1,43 @@ + ['host' => 'localhost', 'user' => 'root'], + DatabaseInterface::DRIVER_MYSQL => ['host' => 'localhost', 'user' => 'root'], ]; /** @@ -52,7 +52,7 @@ private function prepareParams(array $params): array // Set the driver if (!isset($params['driver'])) { - $params['driver'] = Database::DRIVER_MYSQL; + $params['driver'] = DatabaseInterface::DRIVER_MYSQL; } if (isset($this->defaults[$params['driver']])) { diff --git a/src/Dumper/Event/DumpEvent.php b/src/Dumper/Event/DumpEvent.php index 79066155..afa862ec 100644 --- a/src/Dumper/Event/DumpEvent.php +++ b/src/Dumper/Event/DumpEvent.php @@ -5,7 +5,7 @@ namespace Smile\GdprDump\Dumper\Event; use Druidfi\Mysqldump\Mysqldump; -use Smile\GdprDump\Database\Database; +use Smile\GdprDump\Database\DatabaseInterface; use Smile\GdprDump\Dumper\Config\DumperConfigInterface; use Symfony\Contracts\EventDispatcher\Event; @@ -16,7 +16,7 @@ final class DumpEvent extends Event { public function __construct( private Mysqldump $dumper, - private Database $database, + private DatabaseInterface $database, private DumperConfigInterface $config, private array $context ) { @@ -33,7 +33,7 @@ public function getConfig(): DumperConfigInterface /** * Get the database wrapper. */ - public function getDatabase(): Database + public function getDatabase(): DatabaseInterface { return $this->database; }