Skip to content

Commit

Permalink
Add database factory object
Browse files Browse the repository at this point in the history
  • Loading branch information
guvra committed Feb 6, 2024
1 parent 0634f55 commit 55ea19e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
4 changes: 4 additions & 0 deletions app/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 31 additions & 0 deletions src/Database/DatabaseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Smile\GdprDump\Database;

use Smile\GdprDump\Config\ConfigInterface;

class DatabaseFactory
{
/**
* Create a database object.
*/
public function create(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);
}
}
31 changes: 4 additions & 27 deletions src/Dumper/MysqlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = [])
{
}

Expand All @@ -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);

Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit 55ea19e

Please sign in to comment.